Skip to content

Commit

Permalink
Huuuuuuuuuuge userland update and CLI improvement (#153)
Browse files Browse the repository at this point in the history
ты пчела я пчеловод хуй пизда зубной налёт
  • Loading branch information
pimnik98 authored Jul 31, 2024
2 parents 906b653 + 7ba0128 commit c7747f7
Show file tree
Hide file tree
Showing 24 changed files with 429 additions and 156 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ $(KERNEL): $(KERNEL_NEED)
@echo -e '\x1b[32mLINK \x1b[0m' $(KERNEL)
@rm -f $(KERNEL)
@$(LD) $(LDFLAGS) -o $(KERNEL) $(KERNEL_NEED)
@#bash $(BUILD_PREFIX)tools/genmap.sh
@#bash $(BUILD_PREFIX)tools/insertmap.sh
@ls -lh $(KERNEL) # kernel.map
@#-rm kernel.map
@bash $(BUILD_PREFIX)tools/genmap.sh
@bash $(BUILD_PREFIX)tools/insertmap.sh
@ls -lh $(KERNEL) kernel.map
@-rm kernel.map

# Сборка ядра
build: $(SOURCES)
Expand Down
1 change: 1 addition & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ SOURCES=\
kernel/src/sys/lapic.c \
kernel/src/drv/ps2.c \
kernel/src/drv/video/intel.c \
kernel/src/extra/command_parser.c \
kernel/src/kernel.c \
# kernel/src/lib/duktape.c \
kernel/src/toys/piano.c \
Expand Down
4 changes: 2 additions & 2 deletions kernel/include/fs/nvfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ typedef struct {
int DriverFS; ///< Загружен ли драйвер фс?
} __attribute__((packed)) NVFS_DECINFO;

NVFS_DECINFO nvfs_decode(const char* Name);
NVFS_DECINFO* nvfs_decode(const char* Name);
size_t nvfs_read(const char* Name, size_t Offset, size_t Count, void* Buffer);
int nvfs_create(const char* Name, int Mode);
int nvfs_delete(const char* Name, int Mode);
size_t nvfs_write(const char* Name, size_t Offset, size_t Count, const void *Buffer);
FSM_FILE nvfs_info(const char* Name);
FSM_DIR* nvfs_dir(const char* Name);
FSM_DIR* nvfs_dir(const char* Name);
13 changes: 13 additions & 0 deletions kernel/include/lib/command_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "common.h"

typedef struct {
char* original_string;
int argc;
char** argv;
} command_parser_t;

void command_parser_new(command_parser_t* parser, const char* _s);
void command_parser_destroy(command_parser_t* parser);

4 changes: 2 additions & 2 deletions kernel/include/lib/php/explode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

#include <common.h>

uint32_t str_cdsp2(const char a_str[], char del);
char** explode(const char str[], char delimiter);
uint32_t str_cdsp2(const char* a_str, char del);
char** explode(const char str[], char delimiter);
12 changes: 6 additions & 6 deletions kernel/include/sys/file_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ struct fd_info {
};

void file_descriptors_init();
void file_descriptor_allocate(const char *filename, size_t mode, int *out);
void file_descriptor_read(int descriptor_number, size_t count, void* buffer);
void file_descriptor_write(int descriptor_number, size_t count, const void* buffer);
void file_descriptor_close(int descriptor_number);
void file_descriptor_seek(int descriptor_number, ssize_t value, size_t whence);
void file_descriptor_tell(int descriptor_number, int* out);
size_t file_descriptor_allocate(const char *filename, size_t mode, int *out);
size_t file_descriptor_read(int descriptor_number, size_t count, void* buffer);
size_t file_descriptor_write(int descriptor_number, size_t count, const void* buffer);
size_t file_descriptor_close(int descriptor_number);
size_t file_descriptor_seek(int descriptor_number, ssize_t value, size_t whence);
size_t file_descriptor_tell(int descriptor_number, int* out);
97 changes: 68 additions & 29 deletions kernel/src/extra/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../../include/lib/fileio.h"
#include "sys/system.h"
#include "debug/hexview.h"
#include "lib/command_parser.h"

int G_CLI_CURINXA = 0;
int G_CLI_CURINXB = 0;
Expand Down Expand Up @@ -287,7 +288,7 @@ uint32_t CLI_CMD_SET(uint32_t c, char* v[]){
}

uint32_t CLI_CMD_DIR(uint32_t c, char* v[]) {
const char* path = (c <= 1 ? G_CLI_PATH : v[1]);
const char* path = (c == 1 ? G_CLI_PATH : v[1]);

FSM_DIR* Dir = nvfs_dir(path);
if (Dir->Ready != 1){
Expand Down Expand Up @@ -536,18 +537,53 @@ uint32_t CLI_RD(uint32_t argc, char* argv[]) {
return 0;
}

uint32_t CLI_CMD_HEX(uint32_t argc, char** argv) {
if(argc < 2) {
tty_printf("No arguments\n");
return 1;
}

char* file = argv[1];

FILE* fp = fopen(file, "rb");

if(!fp) {
tty_error("Failed to open file: %s\n", file);
return 1;
}

size_t sz = fsize(fp);

char* data = kcalloc(512, 1);

fread(fp, 512, 1, data);

tty_printf("Showing first 512 bytes:\n");

hexview_advanced(data, 512, 26, true, _tty_printf);

kfree(data);
fclose(fp);

return 0;
}

uint32_t CLI_PLAIN(uint32_t argc, char** argv) {
if(argc < 3) {
tty_error("plain <address> <file>");
tty_printf("Note: Address must be in HEX without 0x prefix! Example: CAFEBABE");
return 1;
}

size_t address = htoi(argv[1] + 2);
size_t address = htoi(argv[1]);

qemu_note("Address is: %x", address);

FILE* file = fopen(argv[2], "rb");

size_t filesize = fsize(file);

qemu_log("FILE SIZE IS: %d", filesize);
qemu_note("File size is: %d", filesize);

void* a = kmalloc_common(ALIGN(filesize, PAGE_SIZE), PAGE_SIZE);
memset(a, 0, ALIGN(filesize, PAGE_SIZE));
Expand Down Expand Up @@ -607,43 +643,46 @@ CLI_CMD_ELEM G_CLI_CMD[] = {
{"REBOOT", "reboot", CLI_CMD_REBOOT, "Перезагрузка"},
{"RD", "rd", CLI_RD, "Чтение данных с диска"},
{"SPAWN", "spawn", CLI_SPAWN, "spawn a new process"},
{"ST", "st", CLI_SPAWN_TEST, "spawn test"},
{"PLAIN", "plain", CLI_PLAIN, "Run plain program"},
{"HEX", "hex", CLI_CMD_HEX, "Show hex data"},
{"ST", "st", CLI_SPAWN_TEST, "spawn test"},
{nullptr, nullptr, nullptr}
};

void cli_handler(const char* ncmd){
set_cursor_enabled(0);

uint32_t argc = str_cdsp(ncmd," ") + 1;
char* argv[128] = {0};
command_parser_t parser = {};

str_split(ncmd, argv, " ");
command_parser_new(&parser, ncmd);

for(size_t i = 0; i < argc; i++){
qemu_log("[CLI] '%s' => argc: %d => argv: %s", ncmd, i, argv[i]);
}
for(size_t i = 0; i < parser.argc; i++){
qemu_log("[CLI] '%s' => argc: %d => argv: %s", ncmd, i, parser.argv[i]);
}

bool found = false;

for(size_t i = 0; G_CLI_CMD[i].name != nullptr; i++) {
if(strcmpn(G_CLI_CMD[i].name, argv[0]) || strcmpn(G_CLI_CMD[i].alias, argv[0])) {
G_CLI_CMD[i].funcv(argc, argv);
if(strcmpn(G_CLI_CMD[i].name, parser.argv[0]) || strcmpn(G_CLI_CMD[i].alias, parser.argv[0])) {
G_CLI_CMD[i].funcv(parser.argc, parser.argv);
found = true;
break;
}
}

if(!found) {
CLI_CMD_RUN(argc + 1, argv);
CLI_CMD_RUN(parser.argc, parser.argv);
}

command_parser_destroy(&parser);

set_cursor_enabled(1);
}

void cli(){
qemu_log("[CLI] Started...");
tty_set_bgcolor(0xFF000000);
tty_setcolor(0xFFFFFF);
tty_setcolor(0xFFFFFF);

variable_write("HOSTNAME", "SAYORISOUL");
variable_write("SYSTEMROOT", "R:\\Sayori\\");
Expand All @@ -658,28 +697,28 @@ void cli(){

punch();

char* input_buffer = kcalloc(1, 256);
char* input_buffer = kcalloc(1, 512);
while(1) {
size_t memory_cur = system_heap.used_memory;
size_t memory_cnt_cur = system_heap.allocated_count;
size_t memory_cnt_cur = system_heap.allocated_count;

tty_setcolor(0xFFFFFF);
tty_setcolor(0xFFFFFF);
tty_printf("%s>", G_CLI_PATH);
memset(input_buffer, 0, 256);
memset(input_buffer, 0, 512);

int result = gets_max(input_buffer, 255);
int result = gets_max(input_buffer, 512);

if(result == 1) {
tty_alert("\nMaximum 255 characters!\n");
continue;
}
if(result == 1) {
tty_alert("\nMaximum 512 characters!\n");
continue;
}

size_t len_cmd = strlen(input_buffer);
if (len_cmd == 0) {
continue;
}
size_t len_cmd = strlen(input_buffer);
if (len_cmd == 0) {
continue;
}

size_t current_time = timestamp();
size_t current_time = timestamp();
qemu_log("cmd: %s", input_buffer);

cli_handler(input_buffer);
Expand All @@ -697,7 +736,7 @@ void cli(){
qemu_ok("All right! No memory leaks! Keep it up, buddy!");
}

qemu_note("Time elapsed: %d milliseconds", timestamp() - current_time);
qemu_note("Time elapsed: %d milliseconds", timestamp() - current_time);
}

kfree(input_buffer);
Expand Down
71 changes: 71 additions & 0 deletions kernel/src/extra/command_parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "lib/command_parser.h"
#include "mem/vmm.h"

void command_parser_new(command_parser_t* parser, const char* _s) {
if(!parser) {
return;
}

parser->original_string = kcalloc(strlen(_s) + 1, 1);
memcpy(parser->original_string, _s, strlen(_s));

parser->argc = 0;


char* ptr = parser->original_string;
// Count arguments
while(*ptr++ == ' ')
;
while(*ptr++) {
if(*ptr == ' ' || *ptr == 0) {
if(parser->original_string != ptr - 1) {
parser->argc++;
}

while(*ptr++ == ' ')
;
ptr--;
}
}


parser->argv = kcalloc(parser->argc, sizeof(char*));

char* prev = parser->original_string;
ptr = prev;

size_t cur = 0;

// Parse
while(*ptr++ == ' ')
;

prev = ptr - 1;
while(*ptr++) {
if(*ptr == ' ' || *ptr == 0) {
size_t len = ptr - prev;

parser->argv[cur] = kcalloc(len + 1, 1);

memcpy(parser->argv[cur], prev, len);

cur++;

while(*ptr++ == ' ')
;

prev = --ptr;
}
}
}

void command_parser_destroy(command_parser_t* parser) {
kfree(parser->original_string);

for(int i = 0; i < parser->argc; i++) {
kfree(parser->argv[i]);
}

kfree(parser->argv);
}

Loading

0 comments on commit c7747f7

Please sign in to comment.