2 * Copyright (c) 2007-2008 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 */
49 __unused static const char rcsid[] = "$Sudo: lbuf.c,v 1.7 2008/12/09 20:55:49 millert Exp $";
52 #if !defined(TIOCGSIZE) && defined(TIOCGWINSZ)
53 # define TIOCGSIZE TIOCGWINSZ
54 # define ttysize winsize
55 # define ts_cols ws_col
66 if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) == 0 && win.ts_cols != 0)
67 return((int)win.ts_cols);
70 /* Fall back on $COLUMNS. */
71 if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
77 * TODO: add support for embedded newlines in lbufs
81 lbuf_init(lbuf, buf, indent, continuation)
87 lbuf->continuation = continuation;
88 lbuf->indent = indent;
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 * Print the buffer with word wrap based on the tty width.
214 * The lbuf is reset on return.
221 int i, have, contlen;
222 static int cols = -1;
225 cols = get_ttycols();
226 contlen = lbuf->continuation ? 2 : 0;
228 /* For very small widths just give up... */
229 if (cols <= lbuf->indent + contlen + 20) {
235 * Print the buffer, splitting the line as needed on a word
240 while (cp != NULL && *cp != '\0') {
242 int need = lbuf->len - (int)(cp - lbuf->buf);
245 have -= contlen; /* subtract for continuation char */
246 if ((ep = memrchr(cp, ' ', have)) == NULL)
247 ep = memchr(cp + have, ' ', need - have);
249 need = (int)(ep - cp);
251 if (cp != lbuf->buf) {
252 /* indent continued lines */
253 for (i = 0; i < lbuf->indent; i++)
256 fwrite(cp, need, 1, stdout);
260 * If there is more to print, reset have, incremement cp past
261 * the whitespace, and print a line continuaton char if needed.
264 have = cols - lbuf->indent;
267 } while (isspace((unsigned char)*cp));
268 if (lbuf->continuation) {
270 putchar(lbuf->continuation);
277 lbuf->len = 0; /* reset the buffer for re-use. */