From 1395dc42159d52ba36524fabe805897cd20bd01e Mon Sep 17 00:00:00 2001 From: Aleksa Vučković Date: Fri, 15 Oct 2021 22:41:18 +0200 Subject: Adding string.h & printf() --- src/keyboard.c | 11 +++++----- src/stdio.c | 37 ++++++++++++++++++++++++++++++++ src/stdio.h | 6 ++++++ src/string.c | 58 +++++++++++++++++++++++++++++++++++--------------- src/string.h | 15 +++++++++++++ src/tty.c | 67 +++++++++++++++++++++++++++++++++++++++++++++------------- src/vga.c | 29 +++++++++++++++++++++++-- src/vga.h | 23 -------------------- 8 files changed, 184 insertions(+), 62 deletions(-) create mode 100644 src/stdio.c create mode 100644 src/stdio.h create mode 100644 src/string.h delete mode 100644 src/vga.h (limited to 'src') diff --git a/src/keyboard.c b/src/keyboard.c index 987c7a2..896b61f 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -2,6 +2,7 @@ #include #include #include"keyboard.h" +#include"stdio.h" #define BUFFER_SIZE 200 char buffer[BUFFER_SIZE]; @@ -18,10 +19,10 @@ size_t buffer_index=0; extern char ioport_in(uint8_t port); extern void ioport_out(uint8_t port, char data); -void previous_field(); +void previous_field(void); void terminal_putchar(char c); void tty(char *buffer); -void prompt(); +void prompt(void); void init_keyboard() { @@ -35,7 +36,7 @@ void backspace() if(buffer_index<=0) return; previous_field(); - terminal_putchar(' '); + printf(" "); previous_field(); buffer[--buffer_index]='\0'; return; @@ -43,7 +44,7 @@ void backspace() void enter() { - terminal_putchar('\n'); + printf("\n"); if(buffer_index>0) { tty(buffer); @@ -71,7 +72,7 @@ void handle_keyboard_interrupt() else { buffer[buffer_index++]=keyboard[keycode]; - terminal_putchar(keyboard[keycode]); + printf("%c",keyboard[keycode]); } } diff --git a/src/stdio.c b/src/stdio.c new file mode 100644 index 0000000..31f7dbc --- /dev/null +++ b/src/stdio.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include"string.h" + +void terminal_putchar(char c); +void terminal_writestring(const char* data); +void terminal_writeint(const uint32_t data); + +void printf(char *str, ...) +{ + size_t count=0; + for(size_t i=0;str[i]!='\0';i++) if(str[i]=='%') count++; + + va_list list; + va_start(list, str); + + for(size_t i=0;str[i]!='\0';i++) + { + if(str[i]=='%') + { + i++; + if(str[i]=='c') terminal_putchar(va_arg(list,int)); + else if(str[i]=='s') terminal_writestring(va_arg(list,char*)); + else if(str[i]=='d') terminal_writeint(va_arg(list,int)); + else + { + terminal_writestring("wrong format using print function\n"); + return; + } + } + else terminal_putchar(str[i]); + } + + va_end(list); +} diff --git a/src/stdio.h b/src/stdio.h new file mode 100644 index 0000000..2d0aa8a --- /dev/null +++ b/src/stdio.h @@ -0,0 +1,6 @@ +#ifndef STDIO_H +#define STDIO_H + +void printf(char *str, ...); + +#endif diff --git a/src/string.c b/src/string.c index 4683470..f48614f 100644 --- a/src/string.c +++ b/src/string.c @@ -2,6 +2,15 @@ #include #include +size_t stringlen(char *str) +{ + size_t i; + for(i=0;str[i]!='\0';i++) ; + i--; + + return i; +} + bool stringcmp(char *str1,char *str2) { size_t i; @@ -10,25 +19,40 @@ bool stringcmp(char *str1,char *str2) return 0; } -size_t pieces(char pieces[15][15],char *buffer) +void stringcat(char *str1,char *str2) { - for(size_t x=0;x<15;x++) for(size_t y=0;y<15;y++) pieces[x][y]='\0'; + char *tmp=str1; + while(*tmp) tmp++; + while(*str2) *tmp++=*str2++; +} + +void stringrev(char *str) +{ + size_t i=stringlen(str); - size_t i=0,j=0,r=0; - while(buffer[i]==' '&&buffer[i]!='\0') i++; - for(;buffer[i]!='\0';i++) + for(size_t j=0;j0;num/=10,i++) str[i]='0'+num%10; + str[i]='\0'; + stringrev(str); +} + +uint32_t stoi(char *str) +{ + uint32_t num=0; + for(size_t i=0;str[i]!='\0';i++) { - if(buffer[i]==' ') - { - while(buffer[i]==' '&&buffer[i]!='\0') i++; - j=0; - r++; - i--; - } - else - { - pieces[r][j++]=buffer[i]; - } + num*=10; + num+=str[i]-'0'; } - return r+1; + return num; } diff --git a/src/string.h b/src/string.h new file mode 100644 index 0000000..8aabeff --- /dev/null +++ b/src/string.h @@ -0,0 +1,15 @@ +#ifndef STRING_H +#define STRING_H + +#include +#include +#include + +size_t stringlen(char *str); +bool stringcmp(char *str1,char *str2); +void stringcat(char *str1,char *str2); +void stringrev(char *str); +void itos(char *str,uint32_t num); +uint32_t stoi(char *str); + +#endif diff --git a/src/tty.c b/src/tty.c index c3fe295..bb3965d 100644 --- a/src/tty.c +++ b/src/tty.c @@ -1,32 +1,69 @@ #include #include #include -bool stringcmp(char *str1,char *str2); -size_t pieces(char pieces[][15],char *buffer); + +#include"string.h" +#include"stdio.h" + +#define CMD_LENGTH 20 + void clear(); -void terminal_writestring(const char* data); -void terminal_putchar(char c); -void echo(size_t numberof,char parts[][15]) +size_t pieces(char pieces[][CMD_LENGTH],char *buffer) +{ + for(size_t x=0;x #include #include -#include"vga.h" +#include"string.h" static const size_t VGA_WIDTH = 80; static const size_t VGA_HEIGHT = 25; extern char ioport_in(uint8_t port); extern void ioport_out(uint8_t port, char data); -bool stringcmp(char *str1,char *str2); +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, +}; static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { @@ -80,6 +97,14 @@ void terminal_writestring(const char* data) for(int i=0;data[i]!='\0';i++) terminal_putchar(data[i]); } +void terminal_writeint(const uint32_t num) +{ + char string[100]; + char *str=string; + itos(str,num); + terminal_writestring(str); +} + void prompt() { terminal_writestring("[user@myos] > "); diff --git a/src/vga.h b/src/vga.h deleted file mode 100644 index 8f81c21..0000000 --- a/src/vga.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef VGA -#define VGA - -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_WHITE = 15, -}; - -#endif -- cgit v1.2.3