Add: better dynamic command management
This commit is contained in:
Binary file not shown.
2
makefile
2
makefile
@@ -1,5 +1,5 @@
|
|||||||
CC = i386-elf-7.5.0-Linux-x86_64/bin/i386-elf-gcc
|
CC = i386-elf-7.5.0-Linux-x86_64/bin/i386-elf-gcc
|
||||||
CFLAGS = -Wall -Wextra -Wno-div-by-zero -Wno-builtin-declaration-mismatch -c -I src/
|
CFLAGS = -Wall -Wextra -Wno-builtin-declaration-mismatch -c -I src/
|
||||||
LDFLAGS = -T link.ld -melf_i386
|
LDFLAGS = -T link.ld -melf_i386
|
||||||
AS = nasm
|
AS = nasm
|
||||||
ASFLAGS = -f elf
|
ASFLAGS = -f elf
|
||||||
|
|||||||
@@ -4,59 +4,68 @@
|
|||||||
#include "../programs/programs.h"
|
#include "../programs/programs.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 256
|
#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()
|
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];
|
char input_buffer[BUFFER_SIZE];
|
||||||
colorputs("blankos> ", 9);
|
colorputs("blankos> ", 9);
|
||||||
get_input(input_buffer, BUFFER_SIZE);
|
get_input(input_buffer, BUFFER_SIZE);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
|
|
||||||
// Childish shell
|
if (strcmp(input_buffer, "") == 0) {
|
||||||
if (strcmp(input_buffer, "") == 0)
|
continue;
|
||||||
{
|
|
||||||
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");
|
command();
|
||||||
}
|
} else {
|
||||||
else if (strcmp(input_buffer, "panic") == 0)
|
printf("Unknown command %s\n", input_buffer);
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
int strlen(char* str)
|
int strlen(const char* str)
|
||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while (*str++)
|
while (*str++)
|
||||||
@@ -8,7 +8,7 @@ int strlen(char* str)
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int strcmp(char* str1, char* str2)
|
int strcmp(const char* str1, const char* str2)
|
||||||
{
|
{
|
||||||
while (*str1 && (*str1 == *str2))
|
while (*str1 && (*str1 == *str2))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef INCLUDE_STRING_H
|
#ifndef INCLUDE_STRING_H
|
||||||
#define INCLUDE_STRING_H
|
#define INCLUDE_STRING_H
|
||||||
|
|
||||||
int strlen(char* str);
|
int strlen(const char* str);
|
||||||
int strcmp(char* str1, char* str2);
|
int strcmp(const char* str1, const char* str2);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -39,3 +39,15 @@ void program_uptime()
|
|||||||
double seconds = ticks/18.2065; // PIC channel 0 freq
|
double seconds = ticks/18.2065; // PIC channel 0 freq
|
||||||
printf("%d ticks\t%f seconds\n", ticks, seconds);
|
printf("%d ticks\t%f seconds\n", ticks, seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get help
|
||||||
|
|
||||||
|
void program_help()
|
||||||
|
{
|
||||||
|
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void program_panic()
|
||||||
|
{
|
||||||
|
asm volatile("int $0x13");
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ void program_bf();
|
|||||||
void program_rainbow();
|
void program_rainbow();
|
||||||
void program_clear();
|
void program_clear();
|
||||||
void program_uptime();
|
void program_uptime();
|
||||||
|
void program_panic();
|
||||||
|
void program_help();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user