2011-06-22 00:14:21 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
A thread-safe logging console with a line editor.
|
2011-06-22 00:14:21 -06:00
|
|
|
|
2011-07-15 11:58:17 -06:00
|
|
|
Based on linenoise:
|
|
|
|
linenoise -- guerrilla line editing library against the idea that a
|
2011-07-13 03:45:30 -06:00
|
|
|
line editing lib needs to be 20,000 lines of C code.
|
|
|
|
|
|
|
|
You can find the latest source code at:
|
|
|
|
|
|
|
|
http://github.com/antirez/linenoise
|
|
|
|
|
|
|
|
Does a number of crazy assumptions that happen to be true in 99.9999% of
|
|
|
|
the 2010 UNIX computers around.
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
2011-07-16 00:00:13 -06:00
|
|
|
Copyright (c) 2011, Petr Mrázek <peterix@gmail.com>
|
2011-07-13 03:45:30 -06:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 00:14:21 -06:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2011-07-16 00:00:13 -06:00
|
|
|
#include <iostream>
|
2011-06-22 00:14:21 -06:00
|
|
|
#include <sstream>
|
2011-07-13 03:45:30 -06:00
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2011-07-12 04:13:14 -06:00
|
|
|
#include <termios.h>
|
2011-07-13 03:45:30 -06:00
|
|
|
#include <errno.h>
|
2011-07-13 20:05:27 -06:00
|
|
|
#include <deque>
|
2011-07-26 21:59:09 -06:00
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Hooks.h"
|
2011-06-22 00:14:21 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-07-26 21:59:09 -06:00
|
|
|
#include "tinythread.h"
|
|
|
|
using namespace tthread;
|
|
|
|
|
2011-07-15 07:55:01 -06:00
|
|
|
static int isUnsupportedTerm(void)
|
|
|
|
{
|
|
|
|
static const char *unsupported_term[] = {"dumb","cons25",NULL};
|
|
|
|
char *term = getenv("TERM");
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (term == NULL) return 0;
|
|
|
|
for (j = 0; unsupported_term[j]; j++)
|
|
|
|
if (!strcasecmp(term,unsupported_term[j])) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * ANSI_CLS = "\033[2J";
|
|
|
|
const char * ANSI_BLACK = "\033[22;30m";
|
|
|
|
const char * ANSI_RED = "\033[22;31m";
|
|
|
|
const char * ANSI_GREEN = "\033[22;32m";
|
|
|
|
const char * ANSI_BROWN = "\033[22;33m";
|
|
|
|
const char * ANSI_BLUE = "\033[22;34m";
|
|
|
|
const char * ANSI_MAGENTA = "\033[22;35m";
|
|
|
|
const char * ANSI_CYAN = "\033[22;36m";
|
|
|
|
const char * ANSI_GREY = "\033[22;37m";
|
|
|
|
const char * ANSI_DARKGREY = "\033[01;30m";
|
|
|
|
const char * ANSI_LIGHTRED = "\033[01;31m";
|
|
|
|
const char * ANSI_LIGHTGREEN = "\033[01;32m";
|
|
|
|
const char * ANSI_YELLOW = "\033[01;33m";
|
|
|
|
const char * ANSI_LIGHTBLUE = "\033[01;34m";
|
|
|
|
const char * ANSI_LIGHTMAGENTA = "\033[01;35m";
|
|
|
|
const char * ANSI_LIGHTCYAN = "\033[01;36m";
|
|
|
|
const char * ANSI_WHITE = "\033[01;37m";
|
|
|
|
const char * RESETCOLOR = "\033[0m";
|
|
|
|
|
|
|
|
const char * getANSIColor(const int c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case -1: return RESETCOLOR; // HACK! :P
|
|
|
|
case 0 : return ANSI_BLACK;
|
|
|
|
case 1 : return ANSI_BLUE; // non-ANSI
|
|
|
|
case 2 : return ANSI_GREEN;
|
|
|
|
case 3 : return ANSI_CYAN; // non-ANSI
|
|
|
|
case 4 : return ANSI_RED; // non-ANSI
|
|
|
|
case 5 : return ANSI_MAGENTA;
|
|
|
|
case 6 : return ANSI_BROWN;
|
|
|
|
case 7 : return ANSI_GREY;
|
|
|
|
case 8 : return ANSI_DARKGREY;
|
|
|
|
case 9 : return ANSI_LIGHTBLUE; // non-ANSI
|
|
|
|
case 10: return ANSI_LIGHTGREEN;
|
|
|
|
case 11: return ANSI_LIGHTCYAN; // non-ANSI;
|
|
|
|
case 12: return ANSI_LIGHTRED; // non-ANSI;
|
|
|
|
case 13: return ANSI_LIGHTMAGENTA;
|
|
|
|
case 14: return ANSI_YELLOW; // non-ANSI
|
|
|
|
case 15: return ANSI_WHITE;
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
|
|
|
|
namespace DFHack
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
class Private
|
2011-07-13 03:45:30 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
{
|
2011-07-15 07:55:01 -06:00
|
|
|
dfout_C = NULL;
|
|
|
|
rawmode = false;
|
2012-03-10 02:29:33 -07:00
|
|
|
in_batch = false;
|
2011-07-15 07:55:01 -06:00
|
|
|
supported_terminal = false;
|
|
|
|
state = con_unclaimed;
|
2011-07-13 03:45:30 -06:00
|
|
|
};
|
2011-07-15 16:15:20 -06:00
|
|
|
virtual ~Private()
|
|
|
|
{
|
|
|
|
//sync();
|
|
|
|
}
|
2011-08-13 06:42:09 -06:00
|
|
|
private:
|
|
|
|
bool read_char(unsigned char & out)
|
|
|
|
{
|
2011-10-30 21:17:35 -06:00
|
|
|
FD_ZERO(&descriptor_set);
|
|
|
|
FD_SET(STDIN_FILENO, &descriptor_set);
|
|
|
|
FD_SET(exit_pipe[0], &descriptor_set);
|
|
|
|
int ret = TEMP_FAILURE_RETRY(
|
|
|
|
select (FD_SETSIZE,&descriptor_set, NULL, NULL, NULL)
|
|
|
|
);
|
|
|
|
if(ret == -1)
|
|
|
|
return false;
|
|
|
|
if (FD_ISSET(exit_pipe[0], &descriptor_set))
|
|
|
|
return false;
|
|
|
|
if (FD_ISSET(STDIN_FILENO, &descriptor_set))
|
2011-08-13 06:42:09 -06:00
|
|
|
{
|
2011-10-30 21:17:35 -06:00
|
|
|
// read byte from stdin
|
|
|
|
ret = TEMP_FAILURE_RETRY(
|
|
|
|
read(STDIN_FILENO, &out, 1)
|
|
|
|
);
|
|
|
|
if(ret == -1)
|
2011-08-13 06:42:09 -06:00
|
|
|
return false;
|
2011-10-30 21:17:35 -06:00
|
|
|
return true;
|
2011-08-13 06:42:09 -06:00
|
|
|
}
|
2012-04-20 03:30:37 -06:00
|
|
|
return false;
|
2011-08-13 06:42:09 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
2011-07-15 16:15:20 -06:00
|
|
|
public:
|
2012-03-10 02:29:33 -07:00
|
|
|
void print(const char *data)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
fputs(data, dfout_C);
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void print_text(color_ostream::color_value clr, const std::string &chunk)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
if(!in_batch && state == con_lineedit)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
disable_raw();
|
|
|
|
fprintf(dfout_C,"\x1b[1G");
|
|
|
|
fprintf(dfout_C,"\x1b[0K");
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
color(clr);
|
|
|
|
print(chunk.c_str());
|
|
|
|
|
|
|
|
reset_color();
|
2011-07-15 07:55:01 -06:00
|
|
|
enable_raw();
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
color(clr);
|
|
|
|
print(chunk.c_str());
|
|
|
|
}
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void begin_batch()
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
assert(!in_batch);
|
|
|
|
|
|
|
|
in_batch = true;
|
|
|
|
|
|
|
|
if (state == con_lineedit)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
disable_raw();
|
|
|
|
fprintf(dfout_C,"\x1b[1G");
|
|
|
|
fprintf(dfout_C,"\x1b[0K");
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void end_batch()
|
|
|
|
{
|
|
|
|
assert(in_batch);
|
|
|
|
|
|
|
|
flush();
|
|
|
|
|
|
|
|
in_batch = false;
|
|
|
|
|
|
|
|
if (state == con_lineedit)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
reset_color();
|
2012-03-10 02:29:33 -07:00
|
|
|
enable_raw();
|
|
|
|
prompt_refresh();
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
void flush()
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
if (!rawmode)
|
|
|
|
fflush(dfout_C);
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
2012-03-10 02:29:33 -07:00
|
|
|
|
2011-07-15 07:55:01 -06:00
|
|
|
/// Clear the console, along with its scrollback
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
if(rawmode)
|
|
|
|
{
|
|
|
|
const char * clr = "\033c\033[3J\033[H";
|
|
|
|
::write(STDIN_FILENO,clr,strlen(clr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print("\033c\033[3J\033[H");
|
2011-07-18 08:22:49 -06:00
|
|
|
fflush(dfout_C);
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Position cursor at x,y. 1,1 = top left corner
|
|
|
|
void gotoxy(int x, int y)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
char tmp[64];
|
|
|
|
sprintf(tmp,"\033[%d;%dH", y,x);
|
|
|
|
print(tmp);
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
|
|
|
/// Set color (ANSI color number)
|
|
|
|
void color(Console::color_value index)
|
|
|
|
{
|
|
|
|
if(!rawmode)
|
|
|
|
fprintf(dfout_C,getANSIColor(index));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * colstr = getANSIColor(index);
|
|
|
|
int lstr = strlen(colstr);
|
|
|
|
::write(STDIN_FILENO,colstr,lstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Reset color to default
|
|
|
|
void reset_color(void)
|
|
|
|
{
|
|
|
|
color(Console::COLOR_RESET);
|
|
|
|
if(!rawmode)
|
|
|
|
fflush(dfout_C);
|
|
|
|
}
|
|
|
|
/// Enable or disable the caret/cursor
|
|
|
|
void cursor(bool enable = true)
|
|
|
|
{
|
|
|
|
if(enable)
|
|
|
|
print("\033[?25h");
|
|
|
|
else
|
|
|
|
print("\033[?25l");
|
|
|
|
}
|
|
|
|
/// Waits given number of milliseconds before continuing.
|
|
|
|
void msleep(unsigned int msec);
|
|
|
|
/// get the current number of columns
|
|
|
|
int get_columns(void)
|
|
|
|
{
|
|
|
|
winsize ws;
|
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) return 80;
|
|
|
|
return ws.ws_col;
|
|
|
|
}
|
|
|
|
/// get the current number of rows
|
|
|
|
int get_rows(void)
|
|
|
|
{
|
|
|
|
winsize ws;
|
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) return 25;
|
|
|
|
return ws.ws_row;
|
|
|
|
}
|
|
|
|
/// beep. maybe?
|
|
|
|
//void beep (void);
|
|
|
|
/// A simple line edit (raw mode)
|
2012-03-10 02:29:33 -07:00
|
|
|
int lineedit(const std::string& prompt, std::string& output, recursive_mutex * lock, CommandHistory & ch)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
output.clear();
|
2012-03-10 02:29:33 -07:00
|
|
|
reset_color();
|
2011-07-15 07:55:01 -06:00
|
|
|
this->prompt = prompt;
|
|
|
|
if (!supported_terminal)
|
|
|
|
{
|
|
|
|
print(prompt.c_str());
|
|
|
|
fflush(dfout_C);
|
|
|
|
// FIXME: what do we do here???
|
2012-03-10 02:29:33 -07:00
|
|
|
//SDL_recursive_mutexV(lock);
|
2011-07-15 07:55:01 -06:00
|
|
|
std::getline(std::cin, output);
|
2012-03-10 02:29:33 -07:00
|
|
|
//SDL_recursive_mutexP(lock);
|
2011-07-15 07:55:01 -06:00
|
|
|
return output.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
if (enable_raw() == -1) return 0;
|
|
|
|
if(state == con_lineedit)
|
|
|
|
return -1;
|
|
|
|
state = con_lineedit;
|
2011-08-13 06:42:09 -06:00
|
|
|
count = prompt_loop(lock,ch);
|
2011-07-15 07:55:01 -06:00
|
|
|
state = con_unclaimed;
|
|
|
|
disable_raw();
|
|
|
|
print("\n");
|
|
|
|
if(count != -1)
|
|
|
|
{
|
|
|
|
output = raw_buffer;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int enable_raw()
|
2011-07-13 03:45:30 -06:00
|
|
|
{
|
2011-07-15 07:55:01 -06:00
|
|
|
struct termios raw;
|
|
|
|
|
|
|
|
if (!supported_terminal)
|
|
|
|
return -1;
|
|
|
|
if (tcgetattr(STDIN_FILENO,&orig_termios) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
raw = orig_termios; //modify the original mode
|
|
|
|
// input modes: no break, no CR to NL, no parity check, no strip char,
|
|
|
|
// no start/stop output control.
|
|
|
|
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
|
|
|
// output modes - disable post processing
|
|
|
|
raw.c_oflag &= ~(OPOST);
|
|
|
|
// control modes - set 8 bit chars
|
|
|
|
raw.c_cflag |= (CS8);
|
|
|
|
// local modes - choing off, canonical off, no extended functions,
|
|
|
|
// no signal chars (^Z,^C)
|
2011-08-18 14:09:30 -06:00
|
|
|
#ifdef CONSOLE_NO_CATCH
|
|
|
|
raw.c_lflag &= ~( ECHO | ICANON | IEXTEN );
|
|
|
|
#else
|
|
|
|
raw.c_lflag &= ~( ECHO | ICANON | IEXTEN | ISIG );
|
|
|
|
#endif
|
2011-07-15 07:55:01 -06:00
|
|
|
// control chars - set return condition: min number of bytes and timer.
|
|
|
|
// We want read to return every single byte, without timeout.
|
|
|
|
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0;// 1 byte, no timer
|
|
|
|
// put terminal in raw mode after flushing
|
|
|
|
if (tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw) < 0)
|
|
|
|
return -1;
|
|
|
|
rawmode = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
|
2011-07-15 07:55:01 -06:00
|
|
|
void disable_raw()
|
|
|
|
{
|
|
|
|
/* Don't even check the return value as it's too late. */
|
|
|
|
if (rawmode && tcsetattr(STDIN_FILENO,TCSAFLUSH,&orig_termios) != -1)
|
|
|
|
rawmode = 0;
|
|
|
|
}
|
|
|
|
void prompt_refresh()
|
|
|
|
{
|
|
|
|
char seq[64];
|
|
|
|
int cols = get_columns();
|
|
|
|
int plen = prompt.size();
|
|
|
|
const char * buf = raw_buffer.c_str();
|
|
|
|
int len = raw_buffer.size();
|
|
|
|
int cooked_cursor = raw_cursor;
|
|
|
|
// Use math! This is silly.
|
|
|
|
while((plen+cooked_cursor) >= cols)
|
|
|
|
{
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
cooked_cursor--;
|
|
|
|
}
|
|
|
|
while (plen+len > cols)
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
/* Cursor to left edge */
|
|
|
|
snprintf(seq,64,"\x1b[1G");
|
|
|
|
if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;
|
|
|
|
/* Write the prompt and the current buffer content */
|
|
|
|
if (::write(STDIN_FILENO,prompt.c_str(),plen) == -1) return;
|
|
|
|
if (::write(STDIN_FILENO,buf,len) == -1) return;
|
|
|
|
/* Erase to right */
|
|
|
|
snprintf(seq,64,"\x1b[0K");
|
|
|
|
if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;
|
|
|
|
/* Move cursor to original position. */
|
|
|
|
snprintf(seq,64,"\x1b[1G\x1b[%dC", (int)(cooked_cursor+plen));
|
|
|
|
if (::write(STDIN_FILENO,seq,strlen(seq)) == -1) return;
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
int prompt_loop(recursive_mutex * lock, CommandHistory & history)
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
int fd = STDIN_FILENO;
|
|
|
|
size_t plen = prompt.size();
|
|
|
|
int history_index = 0;
|
|
|
|
raw_buffer.clear();
|
|
|
|
raw_cursor = 0;
|
|
|
|
/* The latest history entry is always our current buffer, that
|
|
|
|
* initially is just an empty string. */
|
|
|
|
const std::string empty;
|
2011-08-13 06:42:09 -06:00
|
|
|
history.add(empty);
|
2011-07-15 07:55:01 -06:00
|
|
|
if (::write(fd,prompt.c_str(),prompt.size()) == -1) return -1;
|
|
|
|
while(1)
|
|
|
|
{
|
2011-08-13 06:42:09 -06:00
|
|
|
unsigned char c;
|
|
|
|
int isok;
|
|
|
|
unsigned char seq[2], seq2;
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->unlock();
|
2011-08-13 06:42:09 -06:00
|
|
|
if(!read_char(c))
|
|
|
|
{
|
|
|
|
lock->lock();
|
|
|
|
return -2;
|
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->lock();
|
2011-07-15 07:55:01 -06:00
|
|
|
/* Only autocomplete when the callback is set. It returns < 0 when
|
|
|
|
* there was an error reading from fd. Otherwise it will return the
|
|
|
|
* character that should be handled next. */
|
|
|
|
if (c == 9)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if( completionCallback != NULL) {
|
|
|
|
c = completeLine(fd,prompt,buf,buflen,&len,&pos,cols);
|
|
|
|
// Return on errors
|
|
|
|
if (c < 0) return len;
|
|
|
|
// Read next character when 0
|
|
|
|
if (c == 0) continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ignore tab
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// just ignore tabs
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 13: // enter
|
2011-08-13 06:42:09 -06:00
|
|
|
history.remove();
|
2011-07-15 07:55:01 -06:00
|
|
|
return raw_buffer.size();
|
|
|
|
case 3: // ctrl-c
|
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
case 127: // backspace
|
|
|
|
case 8: // ctrl-h
|
|
|
|
if (raw_cursor > 0 && raw_buffer.size() > 0)
|
|
|
|
{
|
|
|
|
raw_buffer.erase(raw_cursor-1,1);
|
|
|
|
raw_cursor--;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 27: // escape sequence
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->unlock();
|
2011-08-13 06:42:09 -06:00
|
|
|
if(!read_char(seq[0]) || !read_char(seq[1]))
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->lock();
|
2011-08-13 06:42:09 -06:00
|
|
|
return -2;
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->lock();
|
2011-07-15 07:55:01 -06:00
|
|
|
if(seq[0] == '[')
|
|
|
|
{
|
|
|
|
if (seq[1] == 'D')
|
|
|
|
{
|
|
|
|
left_arrow:
|
|
|
|
if (raw_cursor > 0)
|
|
|
|
{
|
|
|
|
raw_cursor--;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( seq[1] == 'C')
|
|
|
|
{
|
|
|
|
right_arrow:
|
|
|
|
/* right arrow */
|
2012-04-20 03:30:37 -06:00
|
|
|
if (size_t(raw_cursor) != raw_buffer.size())
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
raw_cursor++;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (seq[1] == 'A' || seq[1] == 'B')
|
|
|
|
{
|
|
|
|
/* up and down arrow: history */
|
|
|
|
if (history.size() > 1)
|
|
|
|
{
|
|
|
|
/* Update the current history entry before to
|
|
|
|
* overwrite it with tne next one. */
|
|
|
|
history[history_index] = raw_buffer;
|
|
|
|
/* Show the new entry */
|
|
|
|
history_index += (seq[1] == 'A') ? 1 : -1;
|
|
|
|
if (history_index < 0)
|
|
|
|
{
|
|
|
|
history_index = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-20 03:30:37 -06:00
|
|
|
else if (size_t(history_index) >= history.size())
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
history_index = history.size()-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raw_buffer = history[history_index];
|
|
|
|
raw_cursor = raw_buffer.size();
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(seq[1] == 'H')
|
|
|
|
{
|
|
|
|
// home
|
|
|
|
raw_cursor = 0;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
else if(seq[1] == 'F')
|
|
|
|
{
|
|
|
|
// end
|
|
|
|
raw_cursor = raw_buffer.size();
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
else if (seq[1] > '0' && seq[1] < '7')
|
|
|
|
{
|
|
|
|
// extended escape
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->unlock();
|
2011-08-13 06:42:09 -06:00
|
|
|
if(!read_char(seq2))
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->lock();
|
2011-08-13 06:42:09 -06:00
|
|
|
return -2;
|
2011-07-15 07:55:01 -06:00
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
lock->lock();
|
2011-07-16 02:31:49 -06:00
|
|
|
if (seq[1] == '3' && seq2 == '~' )
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
// delete
|
2012-04-20 03:30:37 -06:00
|
|
|
if (raw_buffer.size() > 0 && size_t(raw_cursor) < raw_buffer.size())
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
raw_buffer.erase(raw_cursor,1);
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-20 03:30:37 -06:00
|
|
|
if (raw_buffer.size() == size_t(raw_cursor))
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
raw_buffer.append(1,c);
|
|
|
|
raw_cursor++;
|
2012-04-20 03:30:37 -06:00
|
|
|
if (plen+raw_buffer.size() < size_t(get_columns()))
|
2011-07-15 07:55:01 -06:00
|
|
|
{
|
|
|
|
/* Avoid a full update of the line in the
|
|
|
|
* trivial case. */
|
|
|
|
if (::write(fd,&c,1) == -1) return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
raw_buffer.insert(raw_cursor,1,c);
|
|
|
|
raw_cursor++;
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 21: // Ctrl+u, delete the whole line.
|
|
|
|
raw_buffer.clear();
|
|
|
|
raw_cursor = 0;
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
case 11: // Ctrl+k, delete from current to end of line.
|
|
|
|
raw_buffer.erase(raw_cursor);
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
case 1: // Ctrl+a, go to the start of the line
|
|
|
|
raw_cursor = 0;
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
case 5: // ctrl+e, go to the end of the line
|
|
|
|
raw_cursor = raw_buffer.size();
|
|
|
|
prompt_refresh();
|
|
|
|
break;
|
|
|
|
case 12: // ctrl+l, clear screen
|
|
|
|
clear();
|
|
|
|
prompt_refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return raw_buffer.size();
|
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
FILE * dfout_C;
|
2011-07-15 07:55:01 -06:00
|
|
|
bool supported_terminal;
|
|
|
|
// state variables
|
|
|
|
bool rawmode; // is raw mode active?
|
|
|
|
termios orig_termios; // saved/restored by raw mode
|
2011-07-15 11:58:17 -06:00
|
|
|
// current state
|
|
|
|
enum console_state
|
|
|
|
{
|
|
|
|
con_unclaimed,
|
|
|
|
con_lineedit
|
|
|
|
} state;
|
2012-03-10 02:29:33 -07:00
|
|
|
bool in_batch;
|
2011-07-15 07:55:01 -06:00
|
|
|
std::string prompt; // current prompt string
|
|
|
|
std::string raw_buffer; // current raw mode buffer
|
|
|
|
int raw_cursor; // cursor position in the buffer
|
2011-08-13 06:42:09 -06:00
|
|
|
// thread exit mechanism
|
|
|
|
int exit_pipe[2];
|
|
|
|
fd_set descriptor_set;
|
2011-07-13 03:45:30 -06:00
|
|
|
};
|
|
|
|
}
|
2011-07-15 07:55:01 -06:00
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
Console::Console()
|
2011-07-13 03:45:30 -06:00
|
|
|
{
|
2011-07-14 00:02:29 -06:00
|
|
|
d = 0;
|
2011-07-17 03:06:45 -06:00
|
|
|
inited = false;
|
|
|
|
// we can't create the mutex at this time. the SDL functions aren't hooked yet.
|
2012-03-10 02:29:33 -07:00
|
|
|
wlock = new recursive_mutex();
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
Console::~Console()
|
|
|
|
{
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
shutdown();
|
|
|
|
if(wlock)
|
2011-07-26 21:59:09 -06:00
|
|
|
delete wlock;
|
2011-07-14 00:02:29 -06:00
|
|
|
if(d)
|
|
|
|
delete d;
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
|
2011-07-31 19:31:52 -06:00
|
|
|
bool Console::init(bool sharing)
|
2011-06-22 00:14:21 -06:00
|
|
|
{
|
2011-07-31 19:31:52 -06:00
|
|
|
if(sharing)
|
|
|
|
{
|
|
|
|
inited = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
freopen("stdout.log", "w", stdout);
|
2011-07-14 00:02:29 -06:00
|
|
|
d = new Private();
|
2011-06-22 00:14:21 -06:00
|
|
|
// make our own weird streams so our IO isn't redirected
|
2011-07-13 03:45:30 -06:00
|
|
|
d->dfout_C = fopen("/dev/tty", "w");
|
|
|
|
std::cin.tie(this);
|
2011-06-22 00:14:21 -06:00
|
|
|
clear();
|
2011-07-15 07:55:01 -06:00
|
|
|
d->supported_terminal = !isUnsupportedTerm() && isatty(STDIN_FILENO);
|
2011-08-13 06:42:09 -06:00
|
|
|
// init the exit mechanism
|
|
|
|
pipe(d->exit_pipe);
|
|
|
|
FD_ZERO(&d->descriptor_set);
|
|
|
|
FD_SET(STDIN_FILENO, &d->descriptor_set);
|
|
|
|
FD_SET(d->exit_pipe[0], &d->descriptor_set);
|
2011-07-17 03:06:45 -06:00
|
|
|
inited = true;
|
2012-01-26 21:54:26 -07:00
|
|
|
return true;
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
|
|
|
|
bool Console::shutdown(void)
|
2011-06-22 00:14:21 -06:00
|
|
|
{
|
2011-07-31 19:31:52 -06:00
|
|
|
if(!d)
|
|
|
|
return true;
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-13 03:45:30 -06:00
|
|
|
if(d->rawmode)
|
2011-07-15 07:55:01 -06:00
|
|
|
d->disable_raw();
|
2011-07-26 21:59:09 -06:00
|
|
|
d->print("\n");
|
2011-07-17 03:06:45 -06:00
|
|
|
inited = false;
|
2011-08-13 06:42:09 -06:00
|
|
|
// kill the thing
|
|
|
|
close(d->exit_pipe[1]);
|
2011-07-17 03:06:45 -06:00
|
|
|
return true;
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
void Console::begin_batch()
|
2011-07-13 03:45:30 -06:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
//color_ostream::begin_batch();
|
2012-03-10 02:29:33 -07:00
|
|
|
|
|
|
|
wlock->lock();
|
|
|
|
|
|
|
|
if (inited)
|
|
|
|
d->begin_batch();
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-10 02:29:33 -07:00
|
|
|
void Console::end_batch()
|
2011-07-14 03:15:23 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
if (inited)
|
|
|
|
d->end_batch();
|
|
|
|
|
|
|
|
wlock->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::flush_proxy()
|
|
|
|
{
|
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
|
|
|
if (inited)
|
|
|
|
d->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::add_text(color_value color, const std::string &text)
|
|
|
|
{
|
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
|
|
|
if (inited)
|
|
|
|
d->print_text(color, text);
|
2011-07-14 03:15:23 -06:00
|
|
|
}
|
|
|
|
|
2011-07-13 03:45:30 -06:00
|
|
|
int Console::get_columns(void)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
int ret = -1;
|
|
|
|
if(inited)
|
|
|
|
ret = d->get_columns();
|
|
|
|
return ret;
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int Console::get_rows(void)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
int ret = -1;
|
|
|
|
if(inited)
|
|
|
|
ret = d->get_rows();
|
|
|
|
return ret;
|
2011-07-13 03:45:30 -06:00
|
|
|
}
|
|
|
|
|
2011-06-22 00:14:21 -06:00
|
|
|
void Console::clear()
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->clear();
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
2011-07-13 03:45:30 -06:00
|
|
|
|
2011-06-22 00:14:21 -06:00
|
|
|
void Console::gotoxy(int x, int y)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->gotoxy(x,y);
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::cursor(bool enable)
|
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
if(inited)
|
|
|
|
d->cursor(enable);
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
|
|
|
|
2011-08-13 06:42:09 -06:00
|
|
|
int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
|
2011-07-13 03:45:30 -06:00
|
|
|
{
|
2012-03-10 02:29:33 -07:00
|
|
|
lock_guard <recursive_mutex> g(*wlock);
|
2011-07-17 03:06:45 -06:00
|
|
|
int ret = -2;
|
|
|
|
if(inited)
|
2011-08-13 06:42:09 -06:00
|
|
|
ret = d->lineedit(prompt,output,wlock,ch);
|
2011-07-15 07:55:01 -06:00
|
|
|
return ret;
|
2011-07-15 11:58:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::msleep (unsigned int msec)
|
|
|
|
{
|
|
|
|
if (msec > 1000) sleep(msec/1000000);
|
|
|
|
usleep((msec % 1000000) * 1000);
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|