kernel/shell.c

63 lines
1.5 KiB
C

#include "nox/stdio.h"
#include "nox/memory.h"
#include "nox/math.h"
#define COMMAND_BUFFER_SIZE 512
int read_command(char* command_buffer) {
char chr = 0;
int pos = 0;
while (chr != '\n') {
chr = getc();
switch (chr) {
case '\b': {
if (pos > 0) pos--;
break;
}
default: {
if (pos < COMMAND_BUFFER_SIZE) {
command_buffer[pos] = chr;
pos++;
}
break;
}
}
}
command_buffer[--pos] = '\0';
return pos;
}
bool handle_command(char* command_buffer) {
int command_length = strlen(command_buffer);
if (command_length < 2) return true;
if (memcmp(command_buffer, "help", min(4, command_length))) {
printf("Commands:\n");
printf(" help | shows this help message\n");
printf(" exit | exit noxsh\n");
} else if (memcmp(command_buffer, "exit", min(4, command_length))) {
printf("exiting noxsh\n");
return false;
} else {
printf("Unknown command: '");
printf(command_buffer);
printf("'\n");
}
return true;
}
void _start() {
printf("Welcome to the nox shell.\ntype 'help' for a list of commands\n");
bool running = true;
while(running) {
printf("\n[/]=> ");
char command_buffer[COMMAND_BUFFER_SIZE];
read_command(command_buffer);
running = handle_command(command_buffer);
}
}