Complete the shell

This commit is contained in:
Baptiste Wicht 2014-02-09 17:30:16 +01:00
parent 0ce6e939d2
commit a0e8075d89
3 changed files with 57 additions and 9 deletions

View File

@ -10,6 +10,46 @@
#include <string.hpp> #include <string.hpp>
#include <algorithms.hpp> #include <algorithms.hpp>
namespace {
void exit_command(const std::vector<std::string>& params);
void echo_command(const std::vector<std::string>& params);
void sleep_command(const std::vector<std::string>& params);
struct command_definition {
const char* name;
void (*function)(const std::vector<std::string>&);
};
command_definition commands[3] = {
{"exit", exit_command},
{"echo", echo_command},
{"sleep", sleep_command},
};
void exit_command(const std::vector<std::string>&){
exit(0);
}
void echo_command(const std::vector<std::string>& params){
for(uint64_t i = 1; i < params.size(); ++i){
print(params[i]);
print(' ');
}
print_line();
}
void sleep_command(const std::vector<std::string>& params){
if(params.size() == 1){
print_line("sleep: missing operand");
} else {
size_t time = std::parse(params[1]);
sleep_ms(time * 1000);
}
}
} //end of anonymous namespace
int main(){ int main(){
char input_buffer[64]; char input_buffer[64];
std::string current_input; std::string current_input;
@ -26,16 +66,16 @@ int main(){
auto params = std::split(current_input);; auto params = std::split(current_input);;
if(params[0] == "exit"){ bool found = false;
exit(0); for(auto& command : commands){
} else if(params[0] == "sleep"){ if(params[0] == command.name){
if(params.size() == 1){ command.function(params);
print_line("sleep: missing operand"); found = true;
} else { break;
size_t time = std::parse(params[1]);
sleep_ms(time * 1000);
} }
} else { }
if(!found){
auto result = exec_and_wait(params[0].c_str()); auto result = exec_and_wait(params[0].c_str());
if(!result.valid()){ if(!result.valid()){
@ -59,4 +99,6 @@ int main(){
current_input += input_buffer; current_input += input_buffer;
} }
} }
__builtin_unreachable();
} }

View File

@ -11,10 +11,12 @@
//TODO Rename in console //TODO Rename in console
#include <types.hpp> #include <types.hpp>
#include <string.hpp>
void print(char c); void print(char c);
void print(const char* s); void print(const char* s);
void print(size_t v); void print(size_t v);
void print(const std::string& s);
void print_line(); void print_line();
void print_line(const char* s); void print_line(const char* s);
void print_line(size_t v); void print_line(size_t v);

View File

@ -28,6 +28,10 @@ void print(size_t v){
: "rax", "rbx"); : "rax", "rbx");
} }
void print(const std::string& s){
return print(s.c_str());
}
void print_line(){ void print_line(){
print('\n'); print('\n');
} }