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 */
52 lbuf_init(struct lbuf *lbuf, int (*output)(const char *),
53 int indent, const char *continuation, int cols)
55 lbuf->output = output;
56 lbuf->continuation = continuation;
57 lbuf->indent = indent;
65 lbuf_destroy(struct lbuf *lbuf)
72 * Parse the format and append strings, only %s and %% escapes are supported.
73 * Any characters in set are quoted with a backslash.
76 lbuf_append_quoted(struct lbuf *lbuf, const char *set, const char *fmt, ...)
83 while (*fmt != '\0') {
85 if (fmt[0] == '%' && fmt[1] == 's') {
86 s = va_arg(ap, char *);
89 /* Assume worst case that all chars must be escaped. */
90 if (lbuf->len + (len * 2) + 1 >= lbuf->size) {
93 } while (lbuf->len + len + 1 >= lbuf->size);
94 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
97 if (*(++fmt) == 's') {
98 while ((cp = strpbrk(s, set)) != NULL) {
100 memcpy(lbuf->buf + lbuf->len, s, len);
102 lbuf->buf[lbuf->len++] = '\\';
103 lbuf->buf[lbuf->len++] = *cp;
108 memcpy(lbuf->buf + lbuf->len, s, len);
115 if (strchr(set, *fmt) != NULL)
116 lbuf->buf[lbuf->len++] = '\\';
117 lbuf->buf[lbuf->len++] = *fmt++;
119 lbuf->buf[lbuf->len] = '\0';
124 * Parse the format and append strings, only %s and %% escapes are supported.
127 lbuf_append(struct lbuf *lbuf, const char *fmt, ...)
134 while (*fmt != '\0') {
136 if (fmt[0] == '%' && fmt[1] == 's') {
137 s = va_arg(ap, char *);
140 if (lbuf->len + len + 1 >= lbuf->size) {
143 } while (lbuf->len + len + 1 >= lbuf->size);
144 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
147 if (*(++fmt) == 's') {
148 memcpy(lbuf->buf + lbuf->len, s, len);
154 lbuf->buf[lbuf->len++] = *fmt++;
156 lbuf->buf[lbuf->len] = '\0';
161 lbuf_println(struct lbuf *lbuf, char *line, int len)
164 int i, have, contlen;
166 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
169 * Print the buffer, splitting the line as needed on a word
174 while (cp != NULL && *cp != '\0') {
176 int need = len - (int)(cp - line);
179 have -= contlen; /* subtract for continuation char */
180 if ((ep = memrchr(cp, ' ', have)) == NULL)
181 ep = memchr(cp + have, ' ', need - have);
183 need = (int)(ep - cp);
186 /* indent continued lines */
187 /* XXX - build up string instead? */
188 for (i = 0; i < lbuf->indent; i++)
191 /* NUL-terminate cp for the output function and restore afterwards */
199 * If there is more to print, reset have, incremement cp past
200 * the whitespace, and print a line continuaton char if needed.
203 have = lbuf->cols - lbuf->indent;
205 while (cp < ep && isblank((unsigned char)*cp)) {
209 lbuf->output(lbuf->continuation);
216 * Print the buffer with word wrap based on the tty width.
217 * The lbuf is reset on return.
220 lbuf_print(struct lbuf *lbuf)
225 if (lbuf->buf == NULL || lbuf->len == 0)
228 /* For very small widths just give up... */
229 len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
230 if (lbuf->cols <= lbuf->indent + len + 20) {
231 lbuf->buf[lbuf->len] = '\0';
232 lbuf->output(lbuf->buf);
236 /* Print each line in the buffer */
237 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
242 len = lbuf->len - (cp - lbuf->buf);
243 if ((ep = memchr(cp, '\n', len)) != NULL)
244 len = (int)(ep - cp);
246 lbuf_println(lbuf, cp, len);
247 cp = ep ? ep + 1 : NULL;
252 lbuf->len = 0; /* reset the buffer for re-use. */