Add: better dynamic command management

This commit is contained in:
xamidev
2024-08-09 11:09:15 +02:00
parent 7b6f3ecd96
commit 3524fdc760
7 changed files with 72 additions and 50 deletions

View File

@@ -4,59 +4,68 @@
#include "../programs/programs.h"
#define BUFFER_SIZE 256
#define MAX_COMMANDS 16
typedef void (*command_func_t)();
typedef struct
{
const char* name;
command_func_t function;
} shell_command_t;
shell_command_t shell_commands[MAX_COMMANDS];
int command_count = 0;
void register_command(const char* name, command_func_t function)
{
if (command_count < MAX_COMMANDS)
{
shell_commands[command_count].name = name;
shell_commands[command_count].function = function;
command_count++;
}
}
command_func_t find_command(const char* name)
{
for (int i=0; i < command_count; i++)
{
if (strcmp(name, shell_commands[i].name) == 0)
return shell_commands[i].function;
}
return 0;
}
void shell_install()
{
while (1) // Bad!!
register_command("help", program_help);
register_command("panic", program_panic);
register_command("words", program_words);
register_command("primes", program_primes);
register_command("rainbow", program_rainbow);
register_command("clear", program_clear);
register_command("math", program_math);
register_command("bf", program_bf);
register_command("uptime", program_uptime);
for (;;)
{
char input_buffer[BUFFER_SIZE];
colorputs("blankos> ", 9);
get_input(input_buffer, BUFFER_SIZE);
puts("\n");
// Childish shell
if (strcmp(input_buffer, "") == 0)
{
continue;
if (strcmp(input_buffer, "") == 0) {
continue;
}
else if (strcmp(input_buffer, "help") == 0)
command_func_t command = find_command(input_buffer);
if (command)
{
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\n");
}
else if (strcmp(input_buffer, "panic") == 0)
{
printf("%d", 4/0);
}
else if (strcmp(input_buffer, "words") == 0)
{
program_words();
}
else if (strcmp(input_buffer, "primes") == 0)
{
program_primes();
}
else if (strcmp(input_buffer, "rainbow") == 0)
{
program_rainbow();
}
else if (strcmp(input_buffer, "clear") == 0)
{
program_clear();
}
else if (strcmp(input_buffer, "math") == 0)
{
program_math();
}
else if (strcmp(input_buffer, "bf") == 0)
{
program_bf();
}
else if (strcmp(input_buffer, "uptime") == 0)
{
program_uptime();
}
else {
printf("Unknown command %s\n", input_buffer);
command();
} else {
printf("Unknown command %s\n", input_buffer);
}
}
}