#include <sys/cdefs.h>
#include <errno.h>
#include "dhcpd.h"
static void do_percentm(char *obuf, size_t size, const char *ibuf);
static char mbuf[1024];
static char fbuf[1024];
int warnings_occurred;
void
error(const 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
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
#endif
if (log_perror) {
write(2, mbuf, strlen(mbuf));
write(2, "\n", 1);
}
cap_syslog(capsyslog, LOG_CRIT, "exiting.");
if (log_perror) {
fprintf(stderr, "exiting.\n");
fflush(stderr);
}
if (pidfile != NULL)
pidfile_remove(pidfile);
exit(1);
}
int
warning(const 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
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
#endif
if (log_perror) {
write(2, mbuf, strlen(mbuf));
write(2, "\n", 1);
}
return (0);
}
int
note(const 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
cap_syslog(capsyslog, log_priority | LOG_INFO, "%s", mbuf);
#endif
if (log_perror) {
write(2, mbuf, strlen(mbuf));
write(2, "\n", 1);
}
return (0);
}
int
debug(const 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
cap_syslog(capsyslog, log_priority | LOG_DEBUG, "%s", mbuf);
#endif
if (log_perror) {
write(2, mbuf, strlen(mbuf));
write(2, "\n", 1);
}
return (0);
}
static void
do_percentm(char *obuf, size_t size, const char *ibuf)
{
char ch;
const char *s = ibuf;
char *t = obuf;
size_t prlen;
size_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 >= 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(const char *fmt, ...)
{
va_list list;
static char spaces[] =
" "
" ";
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
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", token_line);
if (lexline < 81)
cap_syslog(capsyslog, log_priority | LOG_ERR,
"%s^", &spaces[sizeof(spaces) - lexchar]);
#endif
if (log_perror) {
write(2, mbuf, strlen(mbuf));
write(2, "\n", 1);
write(2, token_line, strlen(token_line));
write(2, "\n", 1);
write(2, spaces, lexchar - 1);
write(2, "^\n", 2);
}
warnings_occurred = 1;
return (0);
}