summaryrefslogtreecommitdiff
path: root/library/Console-linux.cpp
diff options
context:
space:
mode:
authorPetr Mrázek2011-07-14 08:02:29 +0200
committerPetr Mrázek2011-07-14 08:02:29 +0200
commitf2b46274ec15b81fc49c393302bad26bf80d63b1 (patch)
treef0ac5c26eeb81b780f47608c5dfbacec38851c9c /library/Console-linux.cpp
parent96153a7b05e7ecfaa0f2c4ab744ae3c71144ff41 (diff)
downloaddfhack-f2b46274ec15b81fc49c393302bad26bf80d63b1.tar.gz
dfhack-f2b46274ec15b81fc49c393302bad26bf80d63b1.tar.bz2
dfhack-f2b46274ec15b81fc49c393302bad26bf80d63b1.tar.xz
Fix for a bug in command history queue access. dfhack script can run valgrind.
Diffstat (limited to 'library/Console-linux.cpp')
-rw-r--r--library/Console-linux.cpp44
1 files changed, 19 insertions, 25 deletions
diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp
index c25e4cdc..3e37b6cd 100644
--- a/library/Console-linux.cpp
+++ b/library/Console-linux.cpp
@@ -118,15 +118,17 @@ namespace DFHack
}
Console::Console():std::ostream(0), std::ios(0)
{
- d = new Private();
+ d = 0;
}
Console::~Console()
{
- delete d;
+ if(d)
+ delete d;
}
bool Console::init(void)
{
+ d = new Private();
// make our own weird streams so our IO isn't redirected
d->dfout_C = fopen("/dev/tty", "w");
d->stream_o = new duthomhas::stdiobuf(d->dfout_C);
@@ -139,7 +141,7 @@ bool Console::shutdown(void)
{
if(d->rawmode)
disable_raw();
- *this << std::endl;
+ print("\n");
}
int Console::print( const char* format, ... )
@@ -174,16 +176,13 @@ void Console::clear()
}
else
{
- *this << "\033c";
- *this << "\033[3J\033[H";
+ print("\033c\033[3J\033[H");
}
}
void Console::gotoxy(int x, int y)
{
- std::ostringstream oss;
- oss << "\033[" << y << ";" << x << "H";
- *this << oss.str();
+ print("\033[%d;%dH", y,x);
}
const char * ANSI_CLS = "\033[2J";
@@ -231,25 +230,20 @@ const char * getANSIColor(const int c)
void Console::color(int index)
{
- *this << getANSIColor(index);
+ print(getANSIColor(index));
}
void Console::reset_color( void )
{
- *this << RESETCOLOR;
+ print(RESETCOLOR);
}
-
void Console::cursor(bool enable)
{
if(enable)
- {
- *this <<"\033[?25h";
- }
+ print("\033[?25h");
else
- {
- *this <<"\033[?25l";
- }
+ print("\033[?25l");
}
void Console::msleep (unsigned int msec)
@@ -339,8 +333,8 @@ int Console::prompt_loop(const std::string & prompt, std::string & buffer)
/* The latest history entry is always our current buffer, that
* initially is just an empty string. */
- history_add("");
-
+ const std::string empty;
+ history_add(empty);
if (::write(fd,prompt.c_str(),plen) == -1) return -1;
while(1)
{
@@ -374,7 +368,8 @@ int Console::prompt_loop(const std::string & prompt, std::string & buffer)
continue;
}
- switch(c) {
+ switch(c)
+ {
case 13: /* enter */
d->history.pop_front();
return buffer.size();
@@ -516,7 +511,7 @@ int Console::prompt_loop(const std::string & prompt, std::string & buffer)
// push to front, remove from back if we are above maximum. ignore immediate duplicates
void Console::history_add(const std::string & command)
{
- if(d->history.front() == command)
+ if(!d->history.empty() && d->history.front() == command)
return;
d->history.push_front(command);
if(d->history.size() > 100)
@@ -527,11 +522,10 @@ int Console::lineedit(const std::string & prompt, std::string & output)
{
output.clear();
int count;
-
if (d->isUnsupportedTerm() || !isatty(STDIN_FILENO))
{
- *this << prompt;
- flush();
+ print(prompt.c_str());
+ fflush(d->dfout_C);
std::getline(std::cin, output);
return output.size();
}
@@ -540,7 +534,7 @@ int Console::lineedit(const std::string & prompt, std::string & output)
if (enable_raw() == -1) return 0;
count = prompt_loop(prompt, output);
disable_raw();
- *this << std::endl;
+ print("\n");
return output.size();
}
} \ No newline at end of file