core: split shell command into individual file and support command arg

This commit is contained in:
starnakin 2024-09-25 21:31:44 +02:00
parent b84d4dbcb8
commit 3a916908ef
13 changed files with 140 additions and 77 deletions

12
headers/commands.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
void halt_cmd(char *arg);
void color_cmd(char *arg);
void date_cmd(char *arg);
void poweroff_cmd(char *arg);
void stack_cmd(char *arg);
void help_cmd(char *arg);
void reboot_cmd(char* arg);
void heap_cmd(char *arg);
void clear_cmd(char *arg);
void merdella_cmd(char *arg);

View File

@ -1,51 +1,5 @@
#pragma once
#include "debug.h"
#include "power.h"
#define PROMPT "> "
[[__maybe_unused__]] static const char *POOP =
" / ____/ / _ \\\n"
" _/ ___/_ / / \\___ \\_\n"
" / _/'-, `---._ / / \\_ \\\n"
" / ______/(0} `, , ` , ) / / \\_ \\\n"
" / V ; ` , ` ( / / ,'~~~~~~`, \\\n"
" | `.____,- ' (, ` , ) / / :`,-'\"\"`. \"; "
"|\n"
" | `-------._); , ` `, / / \\;: )``: |\n"
" / / ) ) ; ` ,, : / / `` : '; "
"\\\n"
"/ / ( (`;: ; ` ;:\\ / / ;;;, "
"\\\n"
"| / (: )``;:;;)`'`'`--./ / ____ _,-';;` "
"|\n"
"| | :` )`;)`)`' : / / ~~~~~ ~~~`--',.;;;| "
"|\n"
"| | `--;~~~~~ ` / /, \" \" \"` \",, \\ ;`` | "
" |\n"
"| | ( ; , / / ; `; ; | "
"|\n"
"| | (; ; ; ` / / ,` ` : | "
"><\n"
"| | (; / / / ` ; ; : |\n"
"| \\ ;(_; ; : / /` ; ; ,,,\"\"\";} `; / "
"><\n"
"\\ \\ : `; `; ` / /,;,'''' );;`); ; / >< "
" ><\n"
" \\ | ;' :; ;/ / (;` :( ; , ; | "
"><\n"
" | | |, `;; ,/ / `)`; `(; ` `; | "
"(`\\\n"
" | \\ ; ;; ``: / `).:` \\;, `. / _> "
")_\n"
" \\ \\_ ,-' ;`;;:;` / ;;'`;; `) )/ ,-' "
",-. `;\n"
" \\ \\_ ~~~,-`;`;,\" / ~~~~~ ,-' ; "
"`\"\"/ /\"\"\n"
" \\_ \\___\"\"\"/\"\" / `\"\"/\"\" "
"\n";
void shell_init(void);
void date(void);
void merdella(void);
void shell_init();

View File

@ -0,0 +1,7 @@
#include "terminal.h"
void clear_cmd(char *arg)
{
(void)arg;
terminal_clear();
}

View File

@ -1,7 +1,7 @@
#include "kprintf.h"
#include "rtc.h"
void date(void)
void date_cmd(char *arg)
{
static const char *months[12] = {"January", "February", "March",
"April", "May", "June",
@ -10,6 +10,7 @@ void date(void)
static const char *week_days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"};
(void)arg;
struct rtc_date date = get_date();
kprintf(0, "%s. %d %s. %d %d:%d:%d\n", week_days[date.index_of_the_day],

View File

@ -0,0 +1,7 @@
#include "power.h"
void halt_cmd(char *arg)
{
(void)arg;
halt();
}

View File

@ -0,0 +1,7 @@
#include "alloc.h"
void heap_cmd(char *arg)
{
(void)arg;
show_alloc_mem();
}

View File

@ -0,0 +1,49 @@
#include "kprintf.h"
#include "shell.h"
[[__maybe_unused__]] static const char *POOP =
" / ____/ / _ \\\n"
" _/ ___/_ / / \\___ \\_\n"
" / _/'-, `---._ / / \\_ \\\n"
" / ______/(0} `, , ` , ) / / \\_ \\\n"
" / V ; ` , ` ( / / ,'~~~~~~`, \\\n"
" | `.____,- ' (, ` , ) / / :`,-'\"\"`. \"; "
"|\n"
" | `-------._); , ` `, / / \\;: )``: |\n"
" / / ) ) ; ` ,, : / / `` : '; "
"\\\n"
"/ / ( (`;: ; ` ;:\\ / / ;;;, "
"\\\n"
"| / (: )``;:;;)`'`'`--./ / ____ _,-';;` "
"|\n"
"| | :` )`;)`)`' : / / ~~~~~ ~~~`--',.;;;| "
"|\n"
"| | `--;~~~~~ ` / /, \" \" \"` \",, \\ ;`` | "
" |\n"
"| | ( ; , / / ; `; ; | "
"|\n"
"| | (; ; ; ` / / ,` ` : | "
"><\n"
"| | (; / / / ` ; ; : |\n"
"| \\ ;(_; ; : / /` ; ; ,,,\"\"\";} `; / "
"><\n"
"\\ \\ : `; `; ` / /,;,'''' );;`); ; / >< "
" ><\n"
" \\ | ;' :; ;/ / (;` :( ; , ; | "
"><\n"
" | | |, `;; ,/ / `)`; `(; ` `; | "
"(`\\\n"
" | \\ ; ;; ``: / `).:` \\;, `. / _> "
")_\n"
" \\ \\_ ,-' ;`;;:;` / ;;'`;; `) )/ ,-' "
",-. `;\n"
" \\ \\_ ~~~,-`;`;,\" / ~~~~~ ,-' ; "
"`\"\"/ /\"\"\n"
" \\_ \\___\"\"\"/\"\" / `\"\"/\"\" "
"\n";
void merdella_cmd(char *arg)
{
(void)arg;
kprintf(POOP);
}

View File

@ -0,0 +1,7 @@
#include "power.h"
void poweroff_cmd(char *arg)
{
(void)arg;
halt();
}

View File

@ -0,0 +1,7 @@
#include "power.h"
void reboot_cmd(char *arg)
{
(void)arg;
reboot();
}

View File

@ -0,0 +1,7 @@
#include "debug.h"
void stack_cmd(char arg)
{
(void)arg;
print_stack();
}

View File

@ -1,39 +1,41 @@
#include "alloc.h"
#include "commands.h"
#include "kprintf.h"
#include "shell.h"
#include "string.h"
#include "terminal.h"
#define NB_CMDS (sizeof(commands) / sizeof(commands[0]))
#define BORDER "==========================================================="
#define HEADER "Welcome to bozOShell - Available Commands"
extern struct screen *screen;
struct shell_command {
char name[16];
char description[256];
void (*fn)(void);
void (*fn)(char *arg);
};
static void help(void);
static const struct shell_command commands[] = {
{"help", "Print this help menu", help},
{"reboot", "Reboot the system", reboot},
{"poweroff", "Shut down the system", halt},
{"halt", "Stop all CPU functions", halt},
{"stack", "Print the stack trace", print_stack},
{"heap", "Print the heaps", show_alloc_mem},
{"clear", "Clear the current terminal", terminal_clear},
{"date", "Display the current time and date (UTC+0)", date},
{"merdella", "Surprise", merdella},
const struct shell_command commands[] = {
{"help", "Print this help menu", help_cmd},
{"reboot", "Reboot the system", reboot_cmd},
{"poweroff", "Shut down the system", poweroff_cmd},
{"halt", "Stop all CPU functions", halt_cmd},
{"stack", "Print the stack trace", stack_cmd},
{"heap", "Print the heaps", heap_cmd},
{"clear", "Clear the current terminal", clear_cmd},
{"date", "Display the current time and date", date_cmd},
{"merdella", "Surprise", merdella_cmd},
// {"color", "Change the screen color", color},
};
static void help(void)
#define NB_CMDS (sizeof(commands) / sizeof(commands[0]))
extern struct screen *screen;
void help_cmd(char *arg)
{
const size_t padding = 15;
(void)arg;
kprintf("%s\n", BORDER);
kprintf(" %s\n", HEADER);
kprintf("%s\n", BORDER);
@ -103,7 +105,7 @@ static void read_line(void)
screen->line[i] = '\0';
}
void shell_init(void)
void shell_init()
{
while (1) {
kprintf(PROMPT);
@ -111,7 +113,8 @@ void shell_init(void)
bool invalid = true;
for (unsigned i = 0; i < NB_CMDS; i++) {
if (!strcmp(commands[i].name, screen->line)) {
commands[i].fn();
commands[i].fn(screen->line +
strlen(commands[i].name) + 1);
invalid = false;
break;
}

View File

@ -1,7 +0,0 @@
#include "kprintf.h"
#include "shell.h"
void merdella(void)
{
kprintf(POOP);
}

View File

@ -33,8 +33,9 @@ void terminal_initialize(void)
for (int i = 0; i < TERM_COUNT; i++) {
screens[i].row = 0;
screens[i].column = 0;
screens[i].color =
screens[i].default_color =
vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
screens[i].color = screens[i].default_color;
memcpy(screens[i].buffer, TERM_BUF, sizeof(screen->buffer));
}
}
@ -59,6 +60,11 @@ void terminal_set_fg_color(uint8_t fg_color)
screen->color = (screen->color & 0xF0) | fg_color;
}
void terminal_set_color(uint8_t color)
{
screen->color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
@ -172,5 +178,8 @@ void set_color_level(int level)
VGA_COLOR_LIGHT_YELLOW, VGA_COLOR_LIGHT_YELLOW,
VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN,
VGA_COLOR_LIGHT_GREY};
if (level == 0)
terminal_set_color(screen->default_color);
terminal_set_fg_color(color_translation[level]);
}