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"
void stack_cmd(char arg)
void stack_cmd(char *arg)
{
(void)arg;
print_stack();
}
}

View File

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