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>
31 #endif /* STDC_HEADERS */
33 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
43 #endif /* HAVE_UNISTD_H */
50 #include "sudo_debug.h"
53 lbuf_init(struct lbuf *lbuf, int (*output)(const char *),
54 int indent, const char *continuation, int cols)
56 debug_decl(lbuf_init, SUDO_DEBUG_UTIL)
58 lbuf->output = output;
59 lbuf->continuation = continuation;
60 lbuf->indent = indent;
70 lbuf_destroy(struct lbuf *lbuf)
72 debug_decl(lbuf_destroy, SUDO_DEBUG_UTIL)
81 * Parse the format and append strings, only %s and %% escapes are supported.
82 * Any characters in set are quoted with a backslash.
85 lbuf_append_quoted(struct lbuf *lbuf, const char *set, const char *fmt, ...)
90 debug_decl(lbuf_append_quoted, SUDO_DEBUG_UTIL)
93 while (*fmt != '\0') {
95 if (fmt[0] == '%' && fmt[1] == 's') {
96 s = va_arg(ap, char *);
99 /* Assume worst case that all chars must be escaped. */
100 if (lbuf->len + (len * 2) + 1 >= lbuf->size) {
103 } while (lbuf->len + len + 1 >= lbuf->size);
104 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
107 if (*(++fmt) == 's') {
108 while ((cp = strpbrk(s, set)) != NULL) {
110 memcpy(lbuf->buf + lbuf->len, s, len);
112 lbuf->buf[lbuf->len++] = '\\';
113 lbuf->buf[lbuf->len++] = *cp;
118 memcpy(lbuf->buf + lbuf->len, s, len);
125 if (strchr(set, *fmt) != NULL)
126 lbuf->buf[lbuf->len++] = '\\';
127 lbuf->buf[lbuf->len++] = *fmt++;
129 lbuf->buf[lbuf->len] = '\0';
136 * Parse the format and append strings, only %s and %% escapes are supported.
139 lbuf_append(struct lbuf *lbuf, const char *fmt, ...)
144 debug_decl(lbuf_append, SUDO_DEBUG_UTIL)
147 while (*fmt != '\0') {
149 if (fmt[0] == '%' && fmt[1] == 's') {
150 s = va_arg(ap, char *);
153 if (lbuf->len + len + 1 >= lbuf->size) {
156 } while (lbuf->len + len + 1 >= lbuf->size);
157 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
160 if (*(++fmt) == 's') {
161 memcpy(lbuf->buf + lbuf->len, s, len);
167 lbuf->buf[lbuf->len++] = *fmt++;
169 lbuf->buf[lbuf->len] = '\0';
176 lbuf_println(struct lbuf *lbuf, char *line, int len)
179 int i, have, contlen;
180 debug_decl(lbuf_println, SUDO_DEBUG_UTIL)
182 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
185 * Print the buffer, splitting the line as needed on a word
190 while (cp != NULL && *cp != '\0') {
192 int need = len - (int)(cp - line);
195 have -= contlen; /* subtract for continuation char */
196 if ((ep = memrchr(cp, ' ', have)) == NULL)
197 ep = memchr(cp + have, ' ', need - have);
199 need = (int)(ep - cp);
202 /* indent continued lines */
203 /* XXX - build up string instead? */
204 for (i = 0; i < lbuf->indent; i++)
207 /* NUL-terminate cp for the output function and restore afterwards */
215 * If there is more to print, reset have, incremement cp past
216 * the whitespace, and print a line continuaton char if needed.
219 have = lbuf->cols - lbuf->indent;
221 while (cp < ep && isblank((unsigned char)*cp)) {
225 lbuf->output(lbuf->continuation);
234 * Print the buffer with word wrap based on the tty width.
235 * The lbuf is reset on return.
238 lbuf_print(struct lbuf *lbuf)
242 debug_decl(lbuf_print, SUDO_DEBUG_UTIL)
244 if (lbuf->buf == NULL || lbuf->len == 0)
247 /* For very small widths just give up... */
248 len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
249 if (lbuf->cols <= lbuf->indent + len + 20) {
250 lbuf->buf[lbuf->len] = '\0';
251 lbuf->output(lbuf->buf);
255 /* Print each line in the buffer */
256 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
261 len = lbuf->len - (cp - lbuf->buf);
262 if ((ep = memchr(cp, '\n', len)) != NULL)
263 len = (int)(ep - cp);
265 lbuf_println(lbuf, cp, len);
266 cp = ep ? ep + 1 : NULL;
271 lbuf->len = 0; /* reset the buffer for re-use. */