// ***************************************************************************** // file name : cmd.cpp // author : Hung Ngo // description : definitions of simple toy commands // ***************************************************************************** #include #include #include // for exit() #include "cmd.h" #include "Lexer.h" #include "term_control.h" #include "error_handling.h" using namespace std; /** * ----------------------------------------------------------------------------- * expects two Tokens: IDENT and STRING * prints the STRING using the color specified by the IDENT token * ----------------------------------------------------------------------------- */ void print_color(Lexer lexer) { // one or both of t1, t2 might be ENDTOK or ERRTOK Token t1 = lexer.next_token(), t2 = lexer.next_token(); // again there's a better way to initialize this in C++11 map color_map; color_map["red"] = RED; color_map["green"] = GREEN; color_map["blue"] = BLUE; color_map["cyan"] = CYAN; if (!lexer.has_more_token() && t1.type == IDENT && t2.type == STRING) { cout << term_cc(color_map[t1.value]) << t2.value << endl; } else { error_return("Syntax : \"foreground [red|green|blue|cyan] \"str\""); } } /** * ----------------------------------------------------------------------------- * terminates the program, ignores all parameters * ----------------------------------------------------------------------------- */ void bye(Lexer lexer) { if (lexer.has_more_token()) { error_return("Syntax error: use bye/exit/quit\n"); } else { exit(0); } }