fix: shell cmds with args where not called the most efficient way and ARRAY_SIZE wasnt right

This commit is contained in:
0x35c 2024-09-25 22:58:25 +02:00
parent 484e013ceb
commit a64262ee13
2 changed files with 26 additions and 34 deletions

View File

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

View File

@ -6,6 +6,7 @@
#include "shell.h" #include "shell.h"
#include "string.h" #include "string.h"
#include "terminal.h" #include "terminal.h"
#include "utils.h"
#define BORDER "===========================================================" #define BORDER "==========================================================="
#define HEADER "Welcome to bozOShell - Available Commands" #define HEADER "Welcome to bozOShell - Available Commands"
@ -14,27 +15,22 @@ struct shell_command {
char name[16]; char name[16];
char description[256]; char description[256];
void (*fn)(char *arg); void (*fn)(char *arg);
uint8_t name_len;
}; };
#define ARRAY_SIZE(array) sizeof(array) / sizeof(array[0]) const struct shell_command cmds[] = {
#define COMMAND_ADDER(name, description, function) \ {"help", "Print this help menu", help_cmd},
{name, description, function, ARRAY_SIZE(name) - 1} {"reboot", "Reboot the system", reboot_cmd},
{"poweroff", "Shut down the system", poweroff_cmd},
const struct shell_command commands[] = { {"halt", "Stop all CPU functions", halt_cmd},
COMMAND_ADDER("help", "Print this help menu", help_cmd), {"stack", "Print the stack trace", stack_cmd},
COMMAND_ADDER("reboot", "Reboot the system", reboot_cmd), {"heap", "Print the heaps", heap_cmd},
COMMAND_ADDER("poweroff", "Shut down the system", poweroff_cmd), {"clear", "Clear the current terminal", clear_cmd},
COMMAND_ADDER("halt", "Stop all CPU functions", halt_cmd), {"date", "Display the current time and date", date_cmd},
COMMAND_ADDER("stack", "Print the stack trace", stack_cmd), {"merdella", "Surprise", merdella_cmd},
COMMAND_ADDER("heap", "Print the heaps", heap_cmd), {"color", "Change the screen color", color_cmd},
COMMAND_ADDER("clear", "Clear the current terminal", clear_cmd),
COMMAND_ADDER("date", "Display the current time and date", date_cmd),
COMMAND_ADDER("merdella", "Surprise", merdella_cmd),
COMMAND_ADDER("color", "Change the screen color", color_cmd),
}; };
#define NB_CMDS (sizeof(commands) / sizeof(commands[0])) #define NB_CMDS ARRAY_SIZE(cmds)
extern struct screen *screen; extern struct screen *screen;
@ -48,11 +44,10 @@ void help_cmd(char *arg)
kprintf("%s\n", BORDER); kprintf("%s\n", BORDER);
for (size_t i = 0; i < NB_CMDS; i++) { for (size_t i = 0; i < NB_CMDS; i++) {
kprintf(" %s", commands[i].name); kprintf(" %s", cmds[i].name);
for (size_t j = 0; j < (padding - strlen(commands[i].name)); for (size_t j = 0; j < (padding - strlen(cmds[i].name)); j++)
j++)
kprintf(" "); kprintf(" ");
kprintf(": %s\n", commands[i].description); kprintf(": %s\n", cmds[i].description);
} }
kprintf("%s\n", BORDER); kprintf("%s\n", BORDER);
@ -65,9 +60,9 @@ static void auto_complete(void)
const char *last_match; const char *last_match;
for (size_t i = 0; i < NB_CMDS; i++) { for (size_t i = 0; i < NB_CMDS; i++) {
if (!strncmp(screen->line, commands[i].name, len)) { if (!strncmp(screen->line, cmds[i].name, len)) {
nb_matches++; nb_matches++;
last_match = commands[i].name; last_match = cmds[i].name;
} }
} }
if (nb_matches != 1) if (nb_matches != 1)
@ -119,15 +114,12 @@ void shell_init()
read_line(); read_line();
bool invalid = true; bool invalid = true;
for (unsigned i = 0; i < NB_CMDS; i++) { for (unsigned i = 0; i < NB_CMDS; i++) {
if (!strncmp(commands[i].name, screen->line, if (!strncmp(cmds[i].name, screen->line,
commands[i].name_len)) { strlen(cmds[i].name))) {
commands[i].fn( char *args = strchr(screen->line, ' ');
screen->line + commands[i].name_len + if (args)
(strlen(screen->line) > args++; // skip the space
commands[i] cmds[i].fn(args);
.name_len)); // "reboot " => arg = ""
// not " " and "reboot" =>
// "" without invalid read
invalid = false; invalid = false;
break; break;
} }