feature: a lot of stuff and got to a working command line ish

This commit is contained in:
2021-02-01 22:31:21 +01:00
parent 468d5968a9
commit 9986d95dbb
22 changed files with 1511 additions and 76 deletions

View File

@@ -14,10 +14,6 @@ int memcpy(char *dst, char *src, int amount) {
return 0;
}
int strcpy(char *dst, char *src) {
return memcpy(dst, src, strlen(src) + 1);
}
int memset(char *dst, char data, int amount) {
for (int i = 0; i < amount; ++i) {
dst[i] = data;
@@ -34,11 +30,9 @@ int itoa(int i, char *target) {
return 0;
}
int strlen(char *str) {
int length = 0;
while (str[length] != 0) {
length++;
int maxi(int a, int b) {
if (a >= b) {
return a;
}
return length;
return b;
}

View File

@@ -10,12 +10,10 @@
int memcpy(char *dst, char *src, int amount);
int strcpy(char *dst, char *src);
int memset(char *dst, char data, int amount);
int itoa(int i, char *target);
int strlen(char *str);
int maxi(int a, int b);
#endif /* KERNEL_LIBC_LIBC_H_ */

30
kernel/libc/readline.c Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by rick on 01-02-21.
//
#include "readline.h"
#include <libc/libc.h>
#include <types.h>
#include <kprint.h>
#include <mem/mem.h>
#include <drivers/keyboard.h>
#define RESULT_SIZE 256
const char* default_msg = "> ";
char* readline(const char *prompt) {
kprint(prompt == NULL ? default_msg : prompt);
char* result = malloc(RESULT_SIZE);
memset(result, 0, RESULT_SIZE);
for (int i = 0; i < RESULT_SIZE; ++i) {
result[i] = getc();
kprint(&result[i]);
if (result[i] == '\n') {
result[i] = 0;
break;
}
}
return result;
}

10
kernel/libc/readline.h Normal file
View File

@@ -0,0 +1,10 @@
//
// Created by rick on 01-02-21.
//
#ifndef NEW_KERNEL_READLINE_H
#define NEW_KERNEL_READLINE_H
char* readline(const char *prompt);
#endif //NEW_KERNEL_READLINE_H

57
kernel/libc/ringqueue.c Normal file
View File

@@ -0,0 +1,57 @@
//
// Created by rick on 30-01-21.
//
#include "ringqueue.h"
#include <libc/libc.h>
#include <mem/mem.h>
#define calc_pos(buffer, index) ((buffer->object_size) * (index))
typedef struct {
int object_size;
int count;
int read_pos;
int write_pos;
void *mem;
} ring_buffer_t;
void *create_buffer(int count, int object_size) {
ring_buffer_t *buffer = malloc(sizeof(ring_buffer_t));
if (buffer == NULL) {
return NULL;
}
buffer->object_size = object_size;
buffer->count = count;
buffer->read_pos = 0;
buffer->write_pos = 0;
buffer->mem = malloc(count * object_size);
if (buffer->mem == NULL) {
free(buffer);
return NULL;
}
return buffer;
};
void free_buffer(void *buffer) {
free(((ring_buffer_t *) buffer)->mem);
free(buffer);
}
void ring_buffer_put(void *buffer, void *item) {
ring_buffer_t *ring_buffer = (ring_buffer_t *) buffer;
// todo check if this write would overwrite the current reading pos
// todo handle the above case
memcpy(ring_buffer->mem + calc_pos(ring_buffer, ring_buffer->write_pos), item, ring_buffer->object_size);
ring_buffer->write_pos++;
};
bool ring_buffer_get(void *buffer, void* target) {
ring_buffer_t *ring_buffer = (ring_buffer_t *) buffer;
if (ring_buffer->read_pos == ring_buffer->write_pos) {
// nothing to read
return false;
}
memcpy(target, ring_buffer->mem + calc_pos(ring_buffer, ring_buffer->read_pos++), ring_buffer->object_size);
return true;
};

19
kernel/libc/ringqueue.h Normal file
View File

@@ -0,0 +1,19 @@
//
// Created by rick on 30-01-21.
//
#ifndef NEW_KERNEL_RINGQUEUE_H
#define NEW_KERNEL_RINGQUEUE_H
#include <types.h>
#include <libc/stdbool.h>
void *create_buffer(int count, int object_size);
void free_buffer(void *buffer);
void ring_buffer_put(void *buffer, void *item);
bool ring_buffer_get(void *buffer, void* target);
#endif //NEW_KERNEL_RINGQUEUE_H

5
kernel/libc/stdbool.c Normal file
View File

@@ -0,0 +1,5 @@
//
// Created by rick on 31-01-21.
//
#include "stdbool.h"

15
kernel/libc/stdbool.h Normal file
View File

@@ -0,0 +1,15 @@
//
// Created by rick on 31-01-21.
//
#ifndef NEW_KERNEL_STDBOOL_H
#define NEW_KERNEL_STDBOOL_H
#define TRUE 1
#define true 1
#define FALSE 0
#define false 0
typedef unsigned char bool;
#endif //NEW_KERNEL_STDBOOL_H

60
kernel/libc/string.c Normal file
View File

@@ -0,0 +1,60 @@
//
// Created by rick on 01-02-21.
//
#include "string.h"
#include "stdbool.h"
#include <libc/libc.h>
#include <types.h>
int strcpy(char *dst, char *src) {
return memcpy(dst, src, strlen(src) + 1);
}
int strlen(const char *str) {
int length = 0;
while (str[length] != 0) {
length++;
}
return length;
}
int strcmp(const char *s1, const char *s2) {
int len1 = strlen(s1);
int len2 = strlen(s1);
return strncmp(s1, s2, maxi(len1, len2));
}
const char *strchr(const char *s, char c) {
int index = 0;
while (true) {
if (s[index] == c) {
return &s[index];
}
if (s[index] == 0) {
return NULL;
}
index++;
}
}
int strncmp(const char *s1, const char *s2, int n) {
for (int i = 0; i < n; ++i) {
if (s1[i] == 0 && s2[i] == 0) {
return 0;
}
if (s1[i] == 0) {
return -1;
}
if (s2[i] == 0) {
return 1;
}
if (s1[i] > s2[i]) {
return -1;
}
if (s2[i] < s1[i]) {
return 1;
}
}
return 0;
}

18
kernel/libc/string.h Normal file
View File

@@ -0,0 +1,18 @@
//
// Created by rick on 01-02-21.
//
#ifndef NEW_KERNEL_STRING_H
#define NEW_KERNEL_STRING_H
int strcpy(char *dst, char *src);
int strlen(const char *str);
const char *strchr(const char *s, char c);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, int n);
#endif //NEW_KERNEL_STRING_H