// cmd_test.cpp : test simple command handling with map #include #include #include "Lexer.h" #include "term_control.h" #include "error_handling.h" #include "cmd.h" using namespace std; /** * ----------------------------------------------------------------------------- * just print a prompt. Note the variable 'flush' which forces the prompt to be * printed right away * ----------------------------------------------------------------------------- */ void prompt() { cout << term_cc(BLUE) << "> " << term_cc() << flush; } /** * ----------------------------------------------------------------------------- * the main body * ----------------------------------------------------------------------------- */ int main() { map cmd_map; // simply add all commands to the map, there's a better way to initialize // in C++11, using g++ 4.4 or later, but Timberlake doesn't have it yet cmd_map["foreground"] = print_color; cmd_map["fg"] = print_color; cmd_map["exit"] = bye; cmd_map["bye"] = bye; cmd_map["quit"] = bye; // read inputs, call appropriate function from the map Lexer lex; string line; Token tok; while (cin) { prompt(); getline(cin, line); lex.set_input(line); if (!lex.has_more_token()) continue; tok = lex.next_token(); if (tok.type != IDENT) { error_return("Syntax error"); continue; } if (cmd_map.find(tok.value) != cmd_map.end()) { cmd_map[tok.value](lex); } else { error_return("Unknown command"); } } return 0; }