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-10-09 23:13:37 -04:00
|
|
|
#include <init.h>
|
|
|
|
#include <list.h>
|
|
|
|
#include <kmalloc.h>
|
2012-09-07 23:53:27 -04:00
|
|
|
#include <print.h>
|
|
|
|
#include <vargs.h>
|
|
|
|
|
2012-10-09 23:13:37 -04:00
|
|
|
struct dlist_node print_funcs_list;
|
|
|
|
|
|
|
|
struct print_func {
|
|
|
|
int (*putc)(char);
|
|
|
|
struct dlist_node list;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On startup, we'd like to have print capabilities before kmalloc is
|
|
|
|
* initialized, so statically allocate one struct print_func to allow for the
|
|
|
|
* registration of one print 'sink' before kmalloc is initialized.
|
|
|
|
*/
|
|
|
|
struct print_func print_func_early;
|
|
|
|
int print_register_func_early(int (*putc)(char)) {
|
|
|
|
if (list_empty(&print_funcs_list)) {
|
|
|
|
print_func_early.putc = putc;
|
|
|
|
insert_before(&print_funcs_list, &print_func_early.list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_register_func(int (*putc)(char)) {
|
|
|
|
struct print_func *pf;
|
|
|
|
if (!(pf = kmalloc(sizeof(struct print_func))))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pf->putc = putc;
|
|
|
|
insert_before(&print_funcs_list, &pf->list);
|
2012-10-04 00:26:35 -04:00
|
|
|
return 0;
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
|
|
|
|
2012-10-09 23:13:37 -04:00
|
|
|
int print_unregister_func(int (*putc)(char)) {
|
|
|
|
struct print_func *it;
|
|
|
|
|
|
|
|
if (list_empty(&print_funcs_list))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for_each_list(it, &print_funcs_list, struct print_func, list) {
|
|
|
|
if (it->putc == putc) {
|
|
|
|
remove(&it->list);
|
|
|
|
|
|
|
|
//only kfree if it was kmalloced
|
|
|
|
if (it != &print_func_early)
|
|
|
|
kfree(it);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_putc(char c) {
|
|
|
|
struct print_func *it;
|
|
|
|
|
|
|
|
if (list_empty(&print_funcs_list))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for_each_list(it, &print_funcs_list, struct print_func, list)
|
|
|
|
it->putc(c);
|
|
|
|
return 0;
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
2012-10-09 23:13:37 -04:00
|
|
|
|
2012-10-04 00:26:35 -04:00
|
|
|
void puts(int (*putc)(char), const char *s)
|
2012-09-07 23:53:27 -04:00
|
|
|
{
|
2012-10-09 23:13:37 -04:00
|
|
|
while (*s) putc(*s++);
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
|
|
|
|
2012-10-04 00:26:35 -04:00
|
|
|
void puti(int (*putc)(char), int i)
|
2012-09-07 23:53:27 -04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-10-04 00:26:35 -04:00
|
|
|
void putx(int (*putc)(char), unsigned int i) {
|
2012-09-10 23:59:30 -04:00
|
|
|
int j;
|
2012-09-07 23:53:27 -04:00
|
|
|
|
2012-10-02 23:07:31 -04:00
|
|
|
puts(putc, "0x");
|
2012-09-07 23:53:27 -04:00
|
|
|
|
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-10-04 00:26:35 -04:00
|
|
|
void putb(int (*putc)(char), unsigned int i) {
|
2012-09-21 11:47:45 -04:00
|
|
|
int j;
|
|
|
|
|
2012-10-02 23:07:31 -04:00
|
|
|
puts(putc, "0b");
|
2012-09-21 11:47:45 -04:00
|
|
|
|
|
|
|
for (j = 0; j < 32; j++) {
|
|
|
|
putc((i>>(31-j)) & 1 ? '1' : '0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-04 00:26:35 -04:00
|
|
|
int _print(int (*putc)(char), char *fmt, va_list arg) {
|
2012-09-07 23:53:27 -04:00
|
|
|
char *c;
|
|
|
|
for (c = fmt; *c; c++) {
|
|
|
|
if (*c == '%') {
|
|
|
|
switch (*++c) {
|
|
|
|
case '%':
|
|
|
|
putc('%');
|
|
|
|
break;
|
|
|
|
case 's':
|
2012-10-02 23:07:31 -04:00
|
|
|
puts(putc, va_arg(arg, char *));
|
2012-09-07 23:53:27 -04:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
putc(va_arg(arg, unsigned int));
|
|
|
|
break;
|
|
|
|
case 'd':
|
2012-10-02 23:07:31 -04:00
|
|
|
puti(putc, va_arg(arg, int));
|
2012-09-07 23:53:27 -04:00
|
|
|
break;
|
|
|
|
case 'x':
|
2012-10-02 23:07:31 -04:00
|
|
|
putx(putc, va_arg(arg, unsigned int));
|
2012-09-07 23:53:27 -04:00
|
|
|
break;
|
2012-09-21 11:47:45 -04:00
|
|
|
case 'b':
|
2012-10-02 23:07:31 -04:00
|
|
|
putb(putc, va_arg(arg, unsigned int));
|
2012-09-21 11:47:45 -04:00
|
|
|
break;
|
2012-09-07 23:53:27 -04:00
|
|
|
default:
|
2012-10-02 23:07:31 -04:00
|
|
|
puts(putc, "\nError: print(): Invalid formatting character: '");
|
2012-09-07 23:53:27 -04:00
|
|
|
putc(*c);
|
2012-10-02 23:07:31 -04:00
|
|
|
puts(putc, "'\n");
|
2012-09-07 23:53:27 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
putc(*c);
|
2012-09-29 23:24:55 -04:00
|
|
|
if (*c == '\n')
|
|
|
|
putc('\r');
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
|
|
|
}
|
2012-10-02 23:07:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print(char *fmt, ...) {
|
|
|
|
int ret;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
2012-10-09 23:13:37 -04:00
|
|
|
ret = _print(&print_putc, fmt, arg);
|
2012-09-07 23:53:27 -04:00
|
|
|
va_end(arg);
|
|
|
|
|
2012-10-02 23:07:31 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-04 00:26:35 -04:00
|
|
|
int print_func(int (*putc)(char), char *fmt, ...) {
|
2012-10-02 23:07:31 -04:00
|
|
|
int ret;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
ret = _print(putc, fmt, arg);
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
return ret;
|
2012-09-07 23:53:27 -04:00
|
|
|
}
|
2012-10-09 23:13:37 -04:00
|
|
|
|
|
|
|
void print_init() {
|
|
|
|
init_list(&print_funcs_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
early_initcall(print_init);
|