add va_list for print

This commit is contained in:
Hojun-Cho 2024-12-05 16:29:49 +09:00
parent 7466f0ff8c
commit 06c375cac5
5 changed files with 83 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
# Prerequisites # Prerequisites
build
*.d *.d
# Object files # Object files

View File

@ -2,7 +2,6 @@
#include "dat.h" #include "dat.h"
#include "fn.h" #include "fn.h"
void (*probe1[])(void) = { void (*probe1[])(void) = {
a20up, a20up,
}; };
@ -31,24 +30,10 @@ machdep(void)
} }
static void static void
puts(char *s)
{
for(;*s; ++s)
putchar(*s);
}
static void
putcn(char *s, int n)
{
for(int i=0; i < n; ++i)
putchar(s[i]);
}
static int
rdline(char *buf, int n) rdline(char *buf, int n)
{ {
char *p = buf; char *p = buf;
char *e = buf + n; char *e = buf + n - 1;
while(p < e){ while(p < e){
int c = getchar(); int c = getchar();
@ -70,18 +55,20 @@ rdline(char *buf, int n)
} }
putchar('\n'); putchar('\n');
done: done:
return p - buf; *p = 0;
} }
void void
boot(int bootdev) boot(int bootdev)
{ {
machdep(); machdep();
print("\n\n===> Hello world <===\n\tBooted on disk 0x%x\n", bootdev);
for(;;){ for(;;){
char buf[8192]; char buf[8192];
puts("\nboot >> "); print("\nboot >> ");
int l = rdline(buf, sizeof(buf)); rdline(buf, sizeof buf);
putcn(buf, l); print("%s", buf);
} }
} }

View File

@ -6,6 +6,7 @@ void pcputc(int dev, int c);
// print.c // print.c
void putchar(int c); void putchar(int c);
int getchar(void); int getchar(void);
void print(const char *fmt, ...);
// a20.c // a20.c
void a20up(void); void a20up(void);

View File

@ -2,13 +2,55 @@
#include "dat.h" #include "dat.h"
#include "fn.h" #include "fn.h"
#define TABWIDTH 8 #define TABWIDTH 4
static void conputc(int c); static void conputc(int c);
static int congetc(void); static int congetc(void);
static void doprint(void (*put)(int), const char *fmt, va_list ap);
static void putint(void (*put)(int), int n, const char *sym, int base);
static int pos = 0; static int pos = 0;
void
print(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
doprint(putchar, fmt, args);
va_end(args);
}
static void
doprint(void (*put)(int), const char *s, va_list ap)
{
while(*s){
if(*s != '%'){
put(*s++);
continue;
}
++s;
switch(*s){
case 'd':
putint(put, va_arg(ap,int), "0123456789", 10);
break;
case 'x':
putint(put, va_arg(ap,int), "0123456789abcdef", 16);
break;
case 'X':
putint(put, va_arg(ap,int), "0123456789ABCDEF", 16);
break;
case 's':
for(char *p=va_arg(ap,char*); *p; ++p)
put(*p);
break;
default:
return;
}
++s;
}
}
int int
getchar(void) getchar(void)
{ {
@ -67,3 +109,19 @@ congetc(void)
{ {
return condev.getc(condev.dev); return condev.getc(condev.dev);
} }
static void
putint(void (*put)(int), int n, const char *sym, int base)
{
int i;
char buf[16];
i = 0;
do{
buf[i++] = n%base;
n /= base;
}while(n);
do{
put((int)sym[(int)buf[--i]]);
}while(i);
}

13
u.h
View File

@ -1,5 +1,6 @@
#define elem(x) ((int)(sizeof(x)/sizeof((x)[0]))) #define elem(x) ((int)(sizeof(x)/sizeof((x)[0])))
#define nil ((void*)0) #define nil ((void*)0)
#define USED(x) if(x);
typedef unsigned short ushort; typedef unsigned short ushort;
typedef unsigned char uchar; typedef unsigned char uchar;
@ -19,3 +20,15 @@ typedef signed char i8;
typedef signed short i16; typedef signed short i16;
typedef signed int i32; typedef signed int i32;
typedef signed long long i64; typedef signed long long i64;
typedef char* va_list;
#define va_start(list, start) ((list) = (char*)((int*)&(start)+1))
#define va_end(list) \
USED(list)
#define va_arg(list, mode) \
((sizeof(mode) == 1) ? \
((list += 4), (mode*)list)[-4]: \
(sizeof(mode) == 2) ? \
((list += 4), (mode*)list)[-2]: \
((list += sizeof(mode)), (mode*)list)[-1])