#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/reboot.h>
#include <sys/msgbuf.h>
#include <sys/proc_internal.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/file_internal.h>
#include <sys/tprintf.h>
#include <sys/syslog.h>
#include <stdarg.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/subr_prf.h>
#include <kern/cpu_number.h>
#include <machine/spl.h>
#include <libkern/libkern.h>
extern void bsd_log_lock(void);
extern void bsd_log_unlock(void);
void _printf(int, struct tty *, const char *, ...);
struct snprintf_arg {
char *str;
size_t remain;
};
extern const char *panicstr;
extern void cnputc(char);
void (*v_putc)(char) = cnputc;
extern struct tty cons;
extern struct tty *constty;
extern int __doprnt(const char *fmt,
va_list argp,
void (*)(int, void *),
void *arg,
int radix);
static void printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size);
extern void logwakeup(void);
extern void halt_cpu(void);
static void
snprintf_func(int ch, void *arg);
struct putchar_args {
int flags;
struct tty *tty;
};
static void putchar(int c, void *arg);
void
uprintf(const char *fmt, ...)
{
struct proc *p = current_proc();
struct putchar_args pca;
va_list ap;
struct session *sessp;
sessp = proc_session(p);
if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
pca.flags = TOTTY;
pca.tty = SESSION_TP(sessp);
if (pca.tty != NULL)
tty_lock(pca.tty);
va_start(ap, fmt);
__doprnt(fmt, ap, putchar, &pca, 10);
va_end(ap);
if (pca.tty != NULL)
tty_unlock(pca.tty);
}
if (sessp != SESSION_NULL)
session_rele(sessp);
}
tpr_t
tprintf_open(struct proc *p)
{
struct session * sessp;
sessp = proc_session(p);
if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
return ((tpr_t)sessp);
}
if (sessp != SESSION_NULL)
session_rele(sessp);
return ((tpr_t) NULL);
}
void
tprintf_close(tpr_t sessp)
{
if (sessp)
session_rele((struct session *) sessp);
}
void
tprintf(tpr_t tpr, const char *fmt, ...)
{
struct session *sess = (struct session *)tpr;
struct tty *tp = TTY_NULL;
int flags = TOLOG;
va_list ap;
struct putchar_args pca;
logpri(LOG_INFO);
if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
tty_lock(tp);
if(ttycheckoutq(tp, 0)) {
flags |= TOTTY;
} else {
tty_unlock(tp);
tp = TTY_NULL;
}
}
pca.flags = flags;
pca.tty = tp;
va_start(ap, fmt);
__doprnt(fmt, ap, putchar, &pca, 10);
va_end(ap);
if (tp != NULL)
tty_unlock(tp);
logwakeup();
}
void
ttyprintf(struct tty *tp, const char *fmt, ...)
{
va_list ap;
if (tp != NULL) {
struct putchar_args pca;
pca.flags = TOTTY;
pca.tty = tp;
va_start(ap, fmt);
__doprnt(fmt, ap, putchar, &pca, 10);
va_end(ap);
}
}
extern int log_open;
void
logpri(int level)
{
struct putchar_args pca;
pca.flags = TOLOG;
pca.tty = NULL;
putchar('<', &pca);
printn((uint32_t)level, 10, TOLOG, (struct tty *)0, 0, 0);
putchar('>', &pca);
}
static void
_logtime(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vaddlog(fmt, ap);
va_end(ap);
}
void
logtime(time_t secs)
{
_logtime(" 0 [Time %ld] [Message ", secs);
}
int
vaddlog(const char *fmt, va_list ap)
{
struct putchar_args pca;
pca.flags = TOLOGLOCKED;
pca.tty = NULL;
if (!log_open) {
pca.flags |= TOCONS;
}
bsd_log_lock();
__doprnt(fmt, ap, putchar, &pca, 10);
bsd_log_unlock();
logwakeup();
return 0;
}
void
_printf(int flags, struct tty *ttyp, const char *format, ...)
{
va_list ap;
struct putchar_args pca;
pca.flags = flags;
pca.tty = ttyp;
if (ttyp != NULL) {
tty_lock(ttyp);
va_start(ap, format);
__doprnt(format, ap, putchar, &pca, 10);
va_end(ap);
tty_unlock(ttyp);
}
}
int
prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
{
struct putchar_args pca;
pca.flags = flags;
pca.tty = ttyp;
__doprnt(fmt, ap, putchar, &pca, 10);
return 0;
}
static void
printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size)
{
char prbuf[11];
char *cp;
struct putchar_args pca;
pca.flags = flags;
pca.tty = ttyp;
if (b == 10 && (int)n < 0) {
putchar('-', &pca);
n = (unsigned)(-(int)n);
}
cp = prbuf;
do {
*cp++ = "0123456789abcdef"[n%b];
n /= b;
} while (n);
if (fld_size) {
for (fld_size -= cp - prbuf; fld_size > 0; fld_size--)
if (zf)
putchar('0', &pca);
else
putchar(' ', &pca);
}
do
putchar(*--cp, &pca);
while (cp > prbuf);
}
void tablefull(const char *tab)
{
log(LOG_ERR, "%s: table is full\n", tab);
}
void
putchar(int c, void *arg)
{
struct putchar_args *pca = arg;
char **sp = (char**) pca->tty;
if (panicstr)
constty = 0;
if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
pca->tty = constty;
pca->flags |= TOTTY;
}
if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
(pca->flags & TOCONS) && pca->tty == constty)
constty = 0;
if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177)
log_putc(c);
if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177)
log_putc_locked(c);
if ((pca->flags & TOCONS) && constty == 0 && c != '\0')
(*v_putc)(c);
if (pca->flags & TOSTR) {
**sp = c;
(*sp)++;
}
}
int
vprintf(const char *fmt, va_list ap)
{
struct putchar_args pca;
pca.flags = TOLOG | TOCONS;
pca.tty = NULL;
__doprnt(fmt, ap, putchar, &pca, 10);
return 0;
}
int
vsprintf(char *buf, const char *cfmt, va_list ap)
{
int retval;
struct snprintf_arg info;
info.str = buf;
info.remain = 999999;
retval = __doprnt(cfmt, ap, snprintf_func, &info, 10);
if (info.remain >= 1) {
*info.str++ = '\0';
}
return 0;
}
int
snprintf(char *str, size_t size, const char *format, ...)
{
int retval;
va_list ap;
va_start(ap, format);
retval = vsnprintf(str, size, format, ap);
va_end(ap);
return(retval);
}
int
vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
struct snprintf_arg info;
int retval;
info.str = str;
info.remain = size;
retval = __doprnt(format, ap, snprintf_func, &info, 10);
if (info.remain >= 1)
*info.str++ = '\0';
return retval;
}
static void
snprintf_func(int ch, void *arg)
{
struct snprintf_arg *const info = arg;
if (info->remain >= 2) {
*info->str++ = ch;
info->remain--;
}
}
int
kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
{
__doprnt(fmt, ap, func, arg, radix);
return 0;
}