2 * Copyright (c) 2007-2013 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>
30 #endif /* STDC_HEADERS */
32 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
49 #include "sudo_debug.h"
52 lbuf_init(struct lbuf *lbuf, int (*output)(const char *),
53 int indent, const char *continuation, int cols)
55 debug_decl(lbuf_init, SUDO_DEBUG_UTIL)
57 lbuf->output = output;
58 lbuf->continuation = continuation;
59 lbuf->indent = indent;
69 lbuf_destroy(struct lbuf *lbuf)
71 debug_decl(lbuf_destroy, SUDO_DEBUG_UTIL)
80 lbuf_expand(struct lbuf *lbuf, size_t extra)
82 if (lbuf->len + extra + 1 >= lbuf->size) {
85 } while (lbuf->len + extra + 1 >= lbuf->size);
86 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
91 * Parse the format and append strings, only %s and %% escapes are supported.
92 * Any characters in set are quoted with a backslash.
95 lbuf_append_quoted(struct lbuf *lbuf, const char *set, const char *fmt, ...)
100 debug_decl(lbuf_append_quoted, SUDO_DEBUG_UTIL)
103 while (*fmt != '\0') {
104 if (fmt[0] == '%' && fmt[1] == 's') {
105 if ((s = va_arg(ap, char *)) == NULL)
107 while ((cp = strpbrk(s, set)) != NULL) {
109 lbuf_expand(lbuf, len + 2);
110 memcpy(lbuf->buf + lbuf->len, s, len);
112 lbuf->buf[lbuf->len++] = '\\';
113 lbuf->buf[lbuf->len++] = *cp;
118 lbuf_expand(lbuf, len);
119 memcpy(lbuf->buf + lbuf->len, s, len);
125 lbuf_expand(lbuf, 2);
126 if (strchr(set, *fmt) != NULL)
127 lbuf->buf[lbuf->len++] = '\\';
128 lbuf->buf[lbuf->len++] = *fmt++;
132 lbuf->buf[lbuf->len] = '\0';
139 * Parse the format and append strings, only %s and %% escapes are supported.
142 lbuf_append(struct lbuf *lbuf, const char *fmt, ...)
147 debug_decl(lbuf_append, SUDO_DEBUG_UTIL)
150 while (*fmt != '\0') {
151 if (fmt[0] == '%' && fmt[1] == 's') {
152 if ((s = va_arg(ap, char *)) == NULL)
155 lbuf_expand(lbuf, len);
156 memcpy(lbuf->buf + lbuf->len, s, len);
161 lbuf_expand(lbuf, 1);
162 lbuf->buf[lbuf->len++] = *fmt++;
166 lbuf->buf[lbuf->len] = '\0';
173 lbuf_println(struct lbuf *lbuf, char *line, int len)
176 int i, have, contlen;
177 debug_decl(lbuf_println, SUDO_DEBUG_UTIL)
179 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
182 * Print the buffer, splitting the line as needed on a word
187 while (cp != NULL && *cp != '\0') {
189 int need = len - (int)(cp - line);
192 have -= contlen; /* subtract for continuation char */
193 if ((ep = memrchr(cp, ' ', have)) == NULL)
194 ep = memchr(cp + have, ' ', need - have);
196 need = (int)(ep - cp);
199 /* indent continued lines */
200 /* XXX - build up string instead? */
201 for (i = 0; i < lbuf->indent; i++)
204 /* NUL-terminate cp for the output function and restore afterwards */
212 * If there is more to print, reset have, incremement cp past
213 * the whitespace, and print a line continuaton char if needed.
216 have = lbuf->cols - lbuf->indent;
218 while (cp < ep && isblank((unsigned char)*cp)) {
222 lbuf->output(lbuf->continuation);
231 * Print the buffer with word wrap based on the tty width.
232 * The lbuf is reset on return.
235 lbuf_print(struct lbuf *lbuf)
239 debug_decl(lbuf_print, SUDO_DEBUG_UTIL)
241 if (lbuf->buf == NULL || lbuf->len == 0)
244 /* For very small widths just give up... */
245 len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
246 if (lbuf->cols <= lbuf->indent + len + 20) {
248 lbuf->buf[lbuf->len] = '\0';
249 lbuf->output(lbuf->buf);
250 if (lbuf->buf[lbuf->len - 1] != '\n')
256 /* Print each line in the buffer */
257 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
262 len = lbuf->len - (cp - lbuf->buf);
263 if ((ep = memchr(cp, '\n', len)) != NULL)
264 len = (int)(ep - cp);
266 lbuf_println(lbuf, cp, len);
267 cp = ep ? ep + 1 : NULL;
272 lbuf->len = 0; /* reset the buffer for re-use. */