2012-09-26 23:59:58 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012, Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
|
|
|
|
This file is part of Aedrix.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-09-07 23:53:27 -04:00
|
|
|
#include <print.h>
|
|
|
|
#include <vargs.h>
|
|
|
|
|
|
|
|
/* This function exists solely so crashes don't happen if putc() gets
|
|
|
|
* called before it is initialized. */
|
|
|
|
void putc_initial(char c) {
|
|
|
|
(void)c;
|
|
|
|
}
|
|
|
|
void (*putc)(char) = &putc_initial;
|
|
|
|
|
|
|
|
void print_init(void (*putcfn)(char)) {
|
|
|
|
if (putcfn)
|
|
|
|
putc = putcfn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void puts(const char *s)
|
|
|
|
{
|
|
|
|
while (*s) putc (*s++);
|
|
|
|
}
|
|
|
|
|
|
|
|
void puti(int i)
|
|
|
|
{
|
|
|
|
unsigned int left;
|
|
|
|
char buf[1 << (sizeof(int)*8) / 10];
|
|
|
|
char *p = buf;
|
|
|
|
unsigned char negative = i < 0;
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
putc('0');
|
|
|
|
|
|
|
|
left = negative ? -1*i : i;
|
|
|
|
|
|
|
|
while (left) {
|
|
|
|
unsigned int remainder = left % 10;
|
|
|
|
left /= 10;
|
|
|
|
*p = ('0'-0) + remainder;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (negative)
|
|
|
|
putc('-');
|
|
|
|
|
|
|
|
while (p-- != buf)
|
|
|
|
putc(*p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void putx(unsigned int i) {
|
2012-09-10 23:59:30 -04:00
|
|
|
int j;
|
2012-09-07 23:53:27 -04:00
|
|
|
|
|
|
|
puts("0x");
|
|
|
|
|
2012-09-10 23:59:30 -04:00
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
unsigned int toprint = (i >> (4*(7-j))) & 0xf;
|
|
|
|
if (toprint < 10)
|
|
|
|
putc(('0'-0) + toprint);
|
2012-09-07 23:53:27 -04:00
|
|
|
else
|
2012-09-10 23:59:30 -04:00
|
|
|
putc(('a'-10) + toprint);
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 11:47:45 -04:00
|
|
|
void putb(unsigned int i) {
|
|
|
|
int j;
|
|
|
|
|
|
|
|
puts("0b");
|
|
|
|
|
|
|
|
for (j = 0; j < 32; j++) {
|
|
|
|
putc((i>>(31-j)) & 1 ? '1' : '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 23:53:27 -04:00
|
|
|
int print(char *fmt, ...) {
|
|
|
|
char *c;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
for (c = fmt; *c; c++) {
|
|
|
|
if (*c == '%') {
|
|
|
|
switch (*++c) {
|
|
|
|
case '%':
|
|
|
|
putc('%');
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
puts(va_arg(arg, char *));
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
putc(va_arg(arg, unsigned int));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
puti(va_arg(arg, int));
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
putx(va_arg(arg, unsigned int));
|
|
|
|
break;
|
2012-09-21 11:47:45 -04:00
|
|
|
case 'b':
|
|
|
|
putb(va_arg(arg, unsigned int));
|
|
|
|
break;
|
2012-09-07 23:53:27 -04:00
|
|
|
default:
|
|
|
|
puts("\nError: print(): Invalid formatting character: '");
|
|
|
|
putc(*c);
|
|
|
|
puts("'\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
putc(*c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|