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 * Append strings to the buffer, expanding it as needed.
75 lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...)
82 while ((s = va_arg(ap, char *)) != NULL) {
84 for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++)
89 /* Expand buffer as needed. */
90 if (lbuf->len + len >= lbuf->size) {
93 } while (lbuf->len + len >= lbuf->size);
94 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
98 /* Append each string. */
99 while ((s = va_arg(ap, char *)) != NULL) {
100 while ((cp = strpbrk(s, set)) != NULL) {
102 memcpy(lbuf->buf + lbuf->len, s, len);
104 lbuf->buf[lbuf->len++] = '\\';
105 lbuf->buf[lbuf->len++] = *cp;
110 memcpy(lbuf->buf + lbuf->len, s, len);
114 lbuf->buf[lbuf->len] = '\0';
119 * Append strings to the buffer, expanding it as needed.
122 lbuf_append(struct lbuf *lbuf, ...)
129 while ((s = va_arg(ap, char *)) != NULL)
133 /* Expand buffer as needed. */
134 if (lbuf->len + len >= lbuf->size) {
137 } while (lbuf->len + len >= lbuf->size);
138 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
142 /* Append each string. */
143 while ((s = va_arg(ap, char *)) != NULL) {
145 memcpy(lbuf->buf + lbuf->len, s, len);
148 lbuf->buf[lbuf->len] = '\0';
153 lbuf_println(struct lbuf *lbuf, char *line, int len)
156 int i, have, contlen;
158 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
161 * Print the buffer, splitting the line as needed on a word
166 while (cp != NULL && *cp != '\0') {
168 int need = len - (int)(cp - line);
171 have -= contlen; /* subtract for continuation char */
172 if ((ep = memrchr(cp, ' ', have)) == NULL)
173 ep = memchr(cp + have, ' ', need - have);
175 need = (int)(ep - cp);
178 /* indent continued lines */
179 /* XXX - build up string instead? */
180 for (i = 0; i < lbuf->indent; i++)
183 /* NUL-terminate cp for the output function and restore afterwards */
191 * If there is more to print, reset have, incremement cp past
192 * the whitespace, and print a line continuaton char if needed.
195 have = lbuf->cols - lbuf->indent;
197 while (cp < ep && isblank((unsigned char)*cp)) {
201 lbuf->output(lbuf->continuation);
208 * Print the buffer with word wrap based on the tty width.
209 * The lbuf is reset on return.
212 lbuf_print(struct lbuf *lbuf)
217 if (lbuf->buf == NULL || lbuf->len == 0)
220 /* For very small widths just give up... */
221 len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
222 if (lbuf->cols <= lbuf->indent + len + 20) {
223 lbuf->buf[lbuf->len] = '\0';
224 lbuf->output(lbuf->buf);
228 /* Print each line in the buffer */
229 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
234 len = lbuf->len - (cp - lbuf->buf);
235 if ((ep = memchr(cp, '\n', len)) != NULL)
236 len = (int)(ep - cp);
238 lbuf_println(lbuf, cp, len);
239 cp = ep ? ep + 1 : NULL;
244 lbuf->len = 0; /* reset the buffer for re-use. */