summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vučković <aleksav013@gmail.com>2021-10-15 22:41:18 +0200
committerAleksa Vučković <aleksav013@gmail.com>2021-10-15 22:41:18 +0200
commit1395dc42159d52ba36524fabe805897cd20bd01e (patch)
tree85ab198e0e2497c70eced46ba1e16e035d48eb24
parent4528b2347ac72ffdabdbe4d9e7bf7789c81e664f (diff)
Adding string.h & printf()
-rw-r--r--Makefile8
-rw-r--r--src/keyboard.c11
-rw-r--r--src/stdio.c37
-rw-r--r--src/stdio.h6
-rw-r--r--src/string.c58
-rw-r--r--src/string.h15
-rw-r--r--src/tty.c67
-rw-r--r--src/vga.c29
-rw-r--r--src/vga.h23
9 files changed, 186 insertions, 68 deletions
diff --git a/Makefile b/Makefile
index 9e7e302..c58cc84 100644
--- a/Makefile
+++ b/Makefile
@@ -15,14 +15,14 @@ ISO_DIR=isodir
TARGET=myos
-OBJ_FILES=boot.o kernel.o gdt.o idt.o keyboard.o vga.o string.o tty.o
+OBJ_FILES=boot.o kernel.o gdt.o idt.o keyboard.o vga.o string.o tty.o stdio.o
CRTBEGIN_OBJ=$(shell $(CC) -print-file-name=crtbegin.o)
CRTEND_OBJ=$(shell $(CC) -print-file-name=crtend.o)
OBJ=$(BUILD_DIR)/crti.o $(CRTBEGIN_OBJ) $(patsubst %,$(BUILD_DIR)/%,$(OBJ_FILES)) $(CRTEND_OBJ) $(BUILD_DIR)/crtn.o
# Default action is set to making kernel binary
-.PHONY: all
+.PHONY: all run run-iso debug clean
all: $(BUILD_DIR)/$(TARGET).bin
# Creating iso file
@@ -48,22 +48,18 @@ $(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c
$(CC) -c $< -o $@ -std=gnu99 $(CFLAGS)
# Boot kernel binary in qemu
-.PHONY: run
run: $(BUILD_DIR)/$(TARGET).bin
$(QEMU) -kernel $^
# Boot iso in qemu
-.PHONY: run-iso
run-iso: $(TARGET).iso
$(QEMU) -cdrom $^
# Debug kernel binary in gdb
-.PHONY: debug
debug: $(TARGET).bin
$(QEMU) -kernel $^ -s -S &
gdb -x .gdbinit
# Clean build files
-.PHONY: clean
clean:
$(RM) $(BUILD_DIR) $(ISO_DIR) $(TARGET).iso
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<stddef.h>
#include<stdint.h>
#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<stdbool.h>
+#include<stddef.h>
+#include<stdint.h>
+#include<stdarg.h>
+#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<stddef.h>
#include<stdint.h>
+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;j<i-j;j++)
+ {
+ char tmp=str[i-j];
+ str[i-j]=str[j];
+ str[j]=tmp;
+ }
+}
+
+void itos(char *str,uint32_t num)
+{
+ size_t i;
+ for(i=0;num>0;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<stdbool.h>
+#include<stddef.h>
+#include<stdint.h>
+
+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<stdbool.h>
#include<stddef.h>
#include<stdint.h>
-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<CMD_LENGTH;x++) for(size_t y=0;y<CMD_LENGTH;y++) pieces[x][y]='\0';
+
+ size_t i=0,j=0,r=0;
+ while(buffer[i]==' '&&buffer[i]!='\0') i++;
+ for(;buffer[i]!='\0';i++)
+ {
+ if(buffer[i]==' ')
+ {
+ while(buffer[i]==' '&&buffer[i]!='\0') i++;
+ j=0;
+ r++;
+ i--;
+ }
+ else
+ {
+ pieces[r][j++]=buffer[i];
+ }
+ }
+ return r+1;
+}
+
+void echo(size_t numberof,char parts[][CMD_LENGTH])
{
for(size_t i=1;i<numberof;i++)
{
- terminal_writestring(parts[i]);
- terminal_putchar(' ');
+ printf("%s ",parts[i]);
}
- terminal_putchar('\n');
+ printf("\n");
+}
+
+void merge(char parts[][CMD_LENGTH])
+{
+ char *str1=parts[1];
+ char *str2=parts[2];
+ stringcat(str1,str2);
+ printf("%s\n",str1);
+}
+
+void ls(size_t numberof,char parts[][CMD_LENGTH])
+{
+ size_t i=numberof;
+ char *part=parts[0];
+ printf("filesystem not implemented yet\n");
}
void tty(char *buffer)
{
- char parts[15][15];
+ char parts[CMD_LENGTH][CMD_LENGTH];
size_t numberof=pieces(parts,buffer);
if(stringcmp(parts[0],"clear")) clear();
else if(stringcmp(parts[0],"echo")) echo(numberof,parts);
- else
- {
- terminal_writestring("command not found: ");
- terminal_writestring(parts[0]);
- terminal_putchar('\n');
- }
+ else if(stringcmp(parts[0],"merge")) merge(parts);
+ else if(stringcmp(parts[0],"ls")) ls(numberof,parts);
+ else if(stringcmp(parts[0],"number")) printf("number times two is %d\n",stoi(parts[1])*2);
+ else printf("command not found: %s\n",parts[0]);
}
diff --git a/src/vga.c b/src/vga.c
index 71faa1a..f176d70 100644
--- a/src/vga.c
+++ b/src/vga.c
@@ -1,14 +1,31 @@
#include<stdbool.h>
#include<stddef.h>
#include<stdint.h>
-#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