#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
# include <memory.h>
# endif
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#else
# ifdef HAVE_TERMIO_H
# include <termio.h>
# endif
#endif
#include "sudo.h"
#include "lbuf.h"
#if !defined(TIOCGWINSZ) && defined(TIOCGSIZE)
# define TIOCGWINSZ TIOCGSIZE
# define winsize ttysize
# define ws_col ts_cols
#endif
int
get_ttycols()
{
char *p;
int cols;
#ifdef TIOCGWINSZ
struct winsize wsize;
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &wsize) == 0 && wsize.ws_col != 0)
return (int)wsize.ws_col;
#endif
if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
cols = 80;
return cols;
}
void
lbuf_init(lbuf, output, indent, continuation)
struct lbuf *lbuf;
int (*output)__P((const char *));
int indent;
const char *continuation;
{
lbuf->output = output;
lbuf->continuation = continuation;
lbuf->indent = indent;
lbuf->cols = get_ttycols();
lbuf->len = 0;
lbuf->size = 0;
lbuf->buf = NULL;
}
void
lbuf_destroy(lbuf)
struct lbuf *lbuf;
{
efree(lbuf->buf);
lbuf->buf = NULL;
}
void
#ifdef __STDC__
lbuf_append_quoted(struct lbuf *lbuf, const char *set, const char *fmt, ...)
#else
lbuf_append_quoted(lbuf, set, fmt, va_alist)
struct lbuf *lbuf;
const char *set;
const char *fmt;
va_dcl
#endif
{
va_list ap;
int len;
char *cp, *s = NULL;
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
while (*fmt != '\0') {
len = 1;
if (fmt[0] == '%' && fmt[1] == 's') {
s = va_arg(ap, char *);
len = strlen(s);
}
if (lbuf->len + (len * 2) + 1 >= lbuf->size) {
do {
lbuf->size += 256;
} while (lbuf->len + len + 1 >= lbuf->size);
lbuf->buf = erealloc(lbuf->buf, lbuf->size);
}
if (*fmt == '%') {
if (*(++fmt) == 's') {
while ((cp = strpbrk(s, set)) != NULL) {
len = (int)(cp - s);
memcpy(lbuf->buf + lbuf->len, s, len);
lbuf->len += len;
lbuf->buf[lbuf->len++] = '\\';
lbuf->buf[lbuf->len++] = *cp;
s = cp + 1;
}
if (*s != '\0') {
len = strlen(s);
memcpy(lbuf->buf + lbuf->len, s, len);
lbuf->len += len;
}
fmt++;
continue;
}
}
if (strchr(set, *fmt) != NULL)
lbuf->buf[lbuf->len++] = '\\';
lbuf->buf[lbuf->len++] = *fmt++;
}
lbuf->buf[lbuf->len] = '\0';
va_end(ap);
}
void
#ifdef __STDC__
lbuf_append(struct lbuf *lbuf, const char *fmt, ...)
#else
lbuf_append(lbuf, fmt, va_alist)
struct lbuf *lbuf;
const char *fmt;
va_dcl
#endif
{
va_list ap;
int len;
char *s = NULL;
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
while (*fmt != '\0') {
len = 1;
if (fmt[0] == '%' && fmt[1] == 's') {
s = va_arg(ap, char *);
len = strlen(s);
}
if (lbuf->len + len + 1 >= lbuf->size) {
do {
lbuf->size += 256;
} while (lbuf->len + len + 1 >= lbuf->size);
lbuf->buf = erealloc(lbuf->buf, lbuf->size);
}
if (*fmt == '%') {
if (*(++fmt) == 's') {
memcpy(lbuf->buf + lbuf->len, s, len);
lbuf->len += len;
fmt++;
continue;
}
}
lbuf->buf[lbuf->len++] = *fmt++;
}
lbuf->buf[lbuf->len] = '\0';
va_end(ap);
}
static void
lbuf_println(lbuf, line, len)
struct lbuf *lbuf;
char *line;
int len;
{
char *cp, save;
int i, have, contlen;
contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
cp = line;
have = lbuf->cols;
while (cp != NULL && *cp != '\0') {
char *ep = NULL;
int need = len - (int)(cp - line);
if (need > have) {
have -= contlen;
if ((ep = memrchr(cp, ' ', have)) == NULL)
ep = memchr(cp + have, ' ', need - have);
if (ep != NULL)
need = (int)(ep - cp);
}
if (cp != line) {
for (i = 0; i < lbuf->indent; i++)
lbuf->output(" ");
}
save = cp[need];
cp[need] = '\0';
lbuf->output(cp);
cp[need] = save;
cp = ep;
if (cp != NULL) {
have = lbuf->cols - lbuf->indent;
ep = line + len;
while (cp < ep && isblank((unsigned char)*cp)) {
cp++;
}
if (contlen)
lbuf->output(lbuf->continuation);
}
lbuf->output("\n");
}
}
void
lbuf_print(lbuf)
struct lbuf *lbuf;
{
char *cp, *ep;
int len;
if (lbuf->buf == NULL || lbuf->len == 0)
goto done;
len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
if (lbuf->cols <= lbuf->indent + len + 20) {
lbuf->buf[lbuf->len] = '\0';
lbuf->output(lbuf->buf);
goto done;
}
for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
if (*cp == '\n') {
lbuf->output("\n");
cp++;
} else {
len = lbuf->len - (cp - lbuf->buf);
if ((ep = memchr(cp, '\n', len)) != NULL)
len = (int)(ep - cp);
if (len)
lbuf_println(lbuf, cp, len);
cp = ep ? ep + 1 : NULL;
}
}
done:
lbuf->len = 0;
}