#include <sys/types.h>
#include <sys/uio.h>
#include <errno.h>
#include <unistd.h>
#include "dhcpd.h"
static void do_percentm(char *obuf, size_t size, char *ibuf);
static char mbuf[1024];
static char fbuf[1024];
int warnings_occurred;
void
error(char *fmt, ...)
{
va_list list;
do_percentm(fbuf, sizeof(fbuf), fmt);
va_start(list, fmt);
vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
va_end(list);
#ifndef DEBUG
syslog(LOG_ERR, "%s", mbuf);
#endif
if (log_perror) {
write(STDERR_FILENO, mbuf, strlen(mbuf));
write(STDERR_FILENO, "\n", 1);
}
if (log_perror) {
fflush(stderr);
}
exit(1);
}
int
warning(char *fmt, ...)
{
va_list list;
do_percentm(fbuf, sizeof(fbuf), fmt);
va_start(list, fmt);
vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
va_end(list);
#ifndef DEBUG
syslog(LOG_ERR, "%s", mbuf);
#endif
if (log_perror) {
write(STDERR_FILENO, mbuf, strlen(mbuf));
write(STDERR_FILENO, "\n", 1);
}
return (0);
}
int
note(char *fmt, ...)
{
va_list list;
do_percentm(fbuf, sizeof(fbuf), fmt);
va_start(list, fmt);
vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
va_end(list);
#ifndef DEBUG
syslog(LOG_INFO, "%s", mbuf);
#endif
if (log_perror) {
write(STDERR_FILENO, mbuf, strlen(mbuf));
write(STDERR_FILENO, "\n", 1);
}
return (0);
}
#ifdef DEBUG
int
debug(char *fmt, ...)
{
va_list list;
do_percentm(fbuf, sizeof(fbuf), fmt);
va_start(list, fmt);
vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
va_end(list);
syslog(LOG_DEBUG, "%s", mbuf);
if (log_perror) {
write(STDERR_FILENO, mbuf, strlen(mbuf));
write(STDERR_FILENO, "\n", 1);
}
return (0);
}
#endif
static void
do_percentm(char *obuf, size_t size, char *ibuf)
{
char ch;
char *s = ibuf;
char *t = obuf;
int prlen;
ssize_t fmt_left;
int saved_errno = errno;
for (fmt_left = size; (ch = *s); ++s) {
if (ch == '%' && s[1] == 'm') {
++s;
prlen = snprintf(t, fmt_left, "%s",
strerror(saved_errno));
if (prlen == -1)
prlen = 0;
else if (prlen >= fmt_left)
prlen = fmt_left - 1;
t += prlen;
fmt_left -= prlen;
} else {
if (fmt_left > 1) {
*t++ = ch;
fmt_left--;
}
}
}
*t = '\0';
}
int
parse_warn(char *fmt, ...)
{
va_list list;
static char spaces[] =
" "
" ";
struct iovec iov[6];
size_t iovcnt;
do_percentm(mbuf, sizeof(mbuf), fmt);
snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
va_start(list, fmt);
vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
va_end(list);
#ifndef DEBUG
syslog(LOG_ERR, "%s", mbuf);
syslog(LOG_ERR, "%s", token_line);
if (lexchar < 81)
syslog(LOG_ERR, "%*c", lexchar, '^');
#endif
if (log_perror) {
iov[0].iov_base = mbuf;
iov[0].iov_len = strlen(mbuf);
iov[1].iov_base = "\n";
iov[1].iov_len = 1;
iov[2].iov_base = token_line;
iov[2].iov_len = strlen(token_line);
iov[3].iov_base = "\n";
iov[3].iov_len = 1;
iovcnt = 4;
if (lexchar < 81) {
iov[4].iov_base = spaces;
iov[4].iov_len = lexchar - 1;
iov[5].iov_base = "^\n";
iov[5].iov_len = 2;
iovcnt += 2;
}
writev(STDERR_FILENO, iov, iovcnt);
}
warnings_occurred = 1;
return (0);
}