From 4f0bc05a74e43cab06a8cdd2fbcc5db0bf46601d Mon Sep 17 00:00:00 2001 From: Marian Ohligs Date: Mon, 23 May 2011 12:56:14 +0200 Subject: [PATCH] add basic support of scanf, get ... --- arch/x86/include/asm/kb.h | 10 ++++++++++ arch/x86/kernel/kb.c | 19 +++++++++++++++++++ drivers/stdin/stdin.c | 19 ++++++++++++++++--- fs/initrd.c | 1 - newlib/examples/hello.c | 21 ++++----------------- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/kb.h b/arch/x86/include/asm/kb.h index 74d66d6c..99991d8b 100644 --- a/arch/x86/include/asm/kb.h +++ b/arch/x86/include/asm/kb.h @@ -43,6 +43,16 @@ extern "C" { */ void keyboard_init(void); +typedef struct +{ + char* buffer; + size_t maxsize; + size_t size; + tid_t tid; +} kb_buffer_t; + +kb_buffer_t kb_buffer; + #endif #ifdef __cplusplus diff --git a/arch/x86/kernel/kb.c b/arch/x86/kernel/kb.c index ac07d146..ce377862 100644 --- a/arch/x86/kernel/kb.c +++ b/arch/x86/kernel/kb.c @@ -24,6 +24,16 @@ #ifdef CONFIG_KEYBOARD +kb_buffer_t kb_buffer = {NULL, 0, 0, 0 }; + +kb_flush() { + kfree(kb_buffer.buffer, (kb_buffer.maxsize * sizeof(char))); + kb_buffer.buffer = NULL; + kb_buffer.size = 0; + kb_buffer.maxsize = 0; + kb_buffer.tid = 0; +} + /* * KBDUS means US Keyboard Layout. This is a scancode table * used to layout a standard US keyboard. I have left some @@ -99,6 +109,15 @@ static void keyboard_handler(struct state *r) * you would add 128 to the scancode when you look for it */ kputchar(kbdus[scancode]); + if (kb_buffer.size <= kb_buffer.maxsize) { + memcpy(kb_buffer.buffer + kb_buffer.size, &kbdus[scancode], 1); + kb_buffer.size++; + } + if (scancode == 28 || scancode == 15 || kb_buffer.size >= kb_buffer.maxsize) { + wakeup_task(kb_buffer.tid); + reschedule(); + } + } } diff --git a/drivers/stdin/stdin.c b/drivers/stdin/stdin.c index 07f7a4df..238f6c24 100644 --- a/drivers/stdin/stdin.c +++ b/drivers/stdin/stdin.c @@ -23,14 +23,27 @@ #include #include #include +#include + /* Implementation of a simple stdin device */ static ssize_t stdin_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) { - while(size) { - size -= kputs((char*)buffer); - } + kb_buffer.buffer = kmalloc(size * sizeof(char)); + kb_buffer.maxsize = size; + kb_buffer.size = 0; + kb_buffer.tid = per_core(current_task)->id; + block_task(per_core(current_task)->id); + reschedule(); + size = kb_buffer.size; + + memcpy(buffer, kb_buffer.buffer, size); + + /*cleaning up */ + kb_flush(); + + //kprintf("Size: %i, offset: %i, buffer: %s", size, buffer, offset); return size; } diff --git a/fs/initrd.c b/fs/initrd.c index ad1aef3e..2c2a7350 100644 --- a/fs/initrd.c +++ b/fs/initrd.c @@ -144,7 +144,6 @@ static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_ * for the next block. */ memcpy(data + offset, buffer, size); - return size; } diff --git a/newlib/examples/hello.c b/newlib/examples/hello.c index c0336af9..fd357d5f 100644 --- a/newlib/examples/hello.c +++ b/newlib/examples/hello.c @@ -28,22 +28,9 @@ extern int errno; int main(int argc, char** argv) { int i; - long offset_x = 5; - char* str = (char *)malloc(50 * sizeof(char)); - FILE* testfile; - testfile = fopen("/bin/test", "w+r"); - setbuf(testfile, NULL); + char* str = (char *)malloc(40000 * sizeof(char)); + scanf("%s", str); + printf("SCANF: %s", str); fflush(NULL); - fwrite("wsx", 3, 1, testfile); - fseek(testfile, 2, SEEK_CUR); - fwrite("nextnextnext", 1, 10, testfile); - - fclose(testfile); - - testfile = fopen("/bin/test", "w+r"); - setbuf(testfile, NULL); - fread(str, 2, 40, testfile); - printf("reading (/bin/test):%s\n", str); - - return errno; +return errno; }