From: Andrew Waterman Date: Tue, 23 Apr 2013 08:43:45 +0000 (-0700) Subject: make interactive mode cope with canonical terminal X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=789abb77fb313b688f83bc292453b1f127188ff0;p=riscv-isa-sim.git make interactive mode cope with canonical terminal --- diff --git a/riscv/interactive.cc b/riscv/interactive.cc index af4c0bd..98e4bb9 100644 --- a/riscv/interactive.cc +++ b/riscv/interactive.cc @@ -8,25 +8,49 @@ #include #include #include +#include +#include +#include +#include + +static std::string readline() +{ + std::string s; + while (1) + { + char ch; + assert(read(1, &ch, 1) == 1); + + if (ch == '\x7f') + { + if (s.empty()) + continue; + s.erase(s.end()-1); + } + + assert(write(1, &ch, 1) == 1); + if (ch == '\n') + return s; + if (ch != '\x7f') + s += ch; + } +} void sim_t::interactive() { - putchar(':'); - char s[128]; - std::cin.getline(s,sizeof(s)-1); + std::cout << ": " << std::flush; + std::string s = readline(); - char* p = strtok(s," "); - if(!p) + std::stringstream ss(s); + std::string cmd, tmp; + std::vector args; + if (!(ss >> cmd)) { interactive_run_noisy(std::string("r"), std::vector(1,"1")); return; } - std::string cmd = p; - - std::vector args; - while((p = strtok(NULL," "))) - args.push_back(p); - + while (ss >> tmp) + args.push_back(tmp); typedef void (sim_t::*interactive_func)(const std::string&, const std::vector&); std::map funcs;