#include <stdarg.h>
#include <sys/unix.h>
#include <sys/devtty.h>
#include <sys/extern.h>

 /*
 |  This prints an error message and dies.
*/
panic( s )
char	*s;
{
	int i;
	kprintf("PANIC: %s\n",s);
	while(1)
		i++;
}


 /*
 |  Displays a warning
*/
int warning( s )
char	*s;
{
	kprintf("WARNING:%s\n",s);
}


 /*
 |  Output a string on the emergency channel
*/
int kputs( s )
char	*s;
{
	int	len=0;
	while( *s )
	{
		kputchar( *s++ );
		len++;
	}
	return len;
}


 /*
 |  Renders an integer as a string in a particular base
 |  Returns the string it creates (space provived by 2nd param)
*/
char *itob( val, str, base )
int	val,
	base;
char	*str;
{
	char	*digits = "0123456789abcdef";
	char	*this = str+7;	/* Start at end of string */
	int	nflag = 0;	/* Add a '-' if this is set */
	*this     = 0;
	if( base<0 )
	{
		base = -base;
		if( val<0 )
		{
			val = -val;
			nflag = 1;
		}
	}
	if( base == 0 )
	{
		warning("tried to kprint() in base 0\n");
		base = 10;
	}
	do {
		*(--this) = *(digits+(val%base));
		val /= base;
	} while( val );
	if( nflag )
		*--this = '-';
		
	return( this );
}


/* Short version of printf to save space */
int kprintf( fmt /* , ... */ )
char	*fmt;
{
	va_list	arg;
	int	c,
		base;
	char	s[7],
		*itob();
	int	len, donelen;

	va_start( arg, fmt );

	while( c = *fmt++ )
	{
		if( c != '%' )
		{
			kputchar(c);
			continue;
		}
		if( *fmt == '-' )
		{
			len = atoi( ++fmt );	/* 0 if nun-numeric */
			if(!len)
				len=1;
			while( c=*fmt++, c>47 && c<58 );
			fmt--;
		}
		else
			len=1;
		switch( c = *fmt++ )
		{
			case 'c':
				kputchar( va_arg(arg,int) );
				donelen=1;
				break;
			case 'd':
			case 'i':
				base = -10;
				goto prt;
			case 'o':
				base = 8;
				goto prt;
			case 'u':
				base = 10;
				goto prt;
			case 'x':
				base = 16;
			prt:
				donelen = kputs( itob( va_arg(arg,int), s, base) );
				break;
			case 's':
				donelen = kputs( va_arg(arg,char*) );
				break;
			default:
				kputchar(c);
				donelen = 1;
				break;
		}
		while( len-(donelen++) > 0 )
			kputchar(' ');
	}
}


