From 789abb77fb313b688f83bc292453b1f127188ff0 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 23 Apr 2013 01:43:45 -0700 Subject: [PATCH] make interactive mode cope with canonical terminal --- riscv/interactive.cc | 46 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) 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; -- 2.30.2