1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/*
$Id: prompt.cc,v 1.29 2006/05/31 19:26:00 peekas Exp $
Command-line editing front-end for arbitrary programs.
Copyright (C) 2001-2009 Kasper Peeters <kasper.peeters@aei.mpg.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
We use some non-standard escape codes:
ESC $ [text] $ : fill the edit buffer with [text]
*/
#include <string>
#include <fstream>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <modglue/main.hh>
#include <modglue/process.hh>
#include <modglue/pipe.hh>
using namespace std;
struct termios saved_attributes, raw_attributes;
modglue::main *cmm;
modglue::opipe *t_stdin;
modglue::ipipe s_stdin("s_stdin");
modglue::opipe s_stdout("s_stdout");
modglue::opipe s_stderr("s_stderr");
pid_t thepid;
bool go_to_sleep=false;
enum state_t { initial, escape_read, movement_escape_read, eat_one_char } state;
string combine, last_output_line;
vector<string> history;
unsigned int hispos=0;
bool moving=false;
unsigned int pos=0;
void sigc_handler(int num)
{
kill(thepid, SIGINT);
signal(SIGINT,sigc_handler);
}
void raw_terminal(void);
void buffered_terminal(void);
void redraw_line(const string& str, unsigned int curpos, unsigned int nextpos);
void sigstp_handler(int num)
{
signal(SIGTSTP,&sigstp_handler);
//go_to_sleep=true;
tcsetattr(0, TCSANOW, &saved_attributes);
kill(getpid(),SIGSTOP);
}
void sigcont_handler(int num)
{
signal(SIGCONT,&sigcont_handler);
tcsetattr(0, TCSANOW, &raw_attributes);
fcntl(0, F_SETFL, O_NONBLOCK);
s_stdout << last_output_line << std::flush;
redraw_line(combine, 0, pos);
}
void buffered_terminal(void)
{
tcsetattr(0, TCSANOW, &saved_attributes);
}
void raw_terminal(void)
{
tcgetattr(0, &saved_attributes);
atexit(buffered_terminal);
tcgetattr(0, &raw_attributes);
raw_attributes.c_lflag &= ~(ICANON|ECHO);
tcsetattr(0, TCSANOW, &raw_attributes);
signal(SIGTSTP,&sigstp_handler);
signal(SIGCONT,&sigcont_handler);
}
// void save_cursor()
// {
// cout << "\033[s";
// }
//
// void restore_cursor()
// {
// cout << "\033[u";
// }
void erase_line()
{
s_stdout << "\033[K";
}
void move_left(unsigned int num=1)
{
if(num>0)
s_stdout << "\033[" << num << "D" << flush;
}
void move_right(unsigned int num=1)
{
if(num>0)
s_stdout << "\033[" << num << "C" << flush;
}
void redraw_line(const string& str, unsigned int curpos, unsigned int nextpos)
{
move_left(curpos);
erase_line();
s_stdout << str << flush;
move_left(str.size()-nextpos);
}
bool died(modglue::ext_process& pr)
{
// s_stderr << "********process " << pr.name() << " died" << endl;
return true;
}
void process_stdin(const char *buffer, int len)
{
for(int curchar=0; curchar<len; ++curchar) {
switch(state) {
case eat_one_char:
state=initial;
break;
case initial:
switch(buffer[curchar]) {
case 1:
redraw_line(combine, pos, 0);
pos=0;
break;
case 5:
redraw_line(combine, pos, combine.size());
pos=combine.size();
break;
case 11:
if(pos<combine.size()) {
combine=combine.substr(0,pos);
redraw_line(combine, pos, pos);
}
break;
case 12:
redraw_line(combine, pos, pos);
break;
case 127:
case 8:
if(pos>0) {
combine=(pos>1?combine.substr(0,pos-1):"")+
(pos<combine.size()-1?combine.substr(pos):"");
redraw_line(combine, pos, pos-1);
--pos;
break;
}
break;
case '\033':
state=escape_read;
// The following line was commented out at some point. Do not do that.
// It disables cursors entirely. What goes wrong?
if(curchar==len-1) --curchar; // no more characters, so this was a real escape.
break;
case '\n':
s_stdout << endl;
history.pop_back();
history.push_back(combine);
history.push_back("");
hispos=history.size()-1;
(*t_stdin) << combine << endl;
pos=0;
combine="";
break;
default:
if(moving) {
(*t_stdin) << buffer[curchar] << flush;
}
else {
if(pos<combine.size()) {
combine=combine.substr(0,pos)+buffer[curchar]+combine.substr(pos);
redraw_line(combine, pos, pos+1);
++pos;
}
else {
s_stdout << buffer[curchar] << flush;
++pos;
combine+=buffer[curchar];
}
}
}
break;
case escape_read:
// s_stdout << "after escape:" << (int)buffer[curchar] << endl;
switch(buffer[curchar]) {
case '\033':
(*t_stdin) << "\\esc" << endl;
state=initial;
break;
case 91:
state=movement_escape_read;
break;
default:
state=initial;
}
break;
case movement_escape_read:
// s_stdout << "after movement_escape:" << (int)buffer[curchar] << endl;
switch(buffer[curchar]) {
case '5':
state=eat_one_char;
if(moving) {
(*t_stdin) << "\\pageup" << endl;
}
break;
case '6':
state=eat_one_char;
if(moving) {
(*t_stdin) << "\\pagedown" << endl;
}
break;
case 'A':
if(moving) {
(*t_stdin) << "\\up" << endl;
}
else {
if(hispos>0) {
if(hispos+1==history.size()) {
history.pop_back();
history.push_back(combine);
}
combine=history[--hispos];
redraw_line(combine, pos, combine.size());
pos=combine.size();
}
}
state=initial;
break;
case 'B':
if(moving) {
(*t_stdin) << "\\down" << endl;
}
else {
if(hispos+1<history.size()) {
combine=history[++hispos];
redraw_line(combine, pos, combine.size());
pos=combine.size();
}
}
state=initial;
break;
case 'C':
if(moving) {
(*t_stdin) << "\\right" << endl;
}
else {
if(pos<combine.size()) {
++pos;
move_right();
}
}
state=initial;
break;
case 'D':
if(moving) {
(*t_stdin) << "\\left" << endl;
}
else {
if(pos>0) {
--pos;
move_left();
}
}
state=initial;
break;
default:
state=initial;
break;
}
break;
}
}
}
bool receive_stdout(modglue::ipipe& p)
{
char buffer[1024];
int len;
enum write_t { w_normal, w_fill };
static write_t wmode=w_normal;
do {
p.read(buffer,1023);
len=p.gcount();
if(len>0) {
buffer[len]=0;
if(len>=3) {
if(strncmp("\033[u", &(buffer[len-3]), 3)==0)
moving=true;
}
for(unsigned int i=0; i<len; ++i) {
switch(wmode) {
case w_normal:
if(buffer[i]=='\033' && (i<len-2 && buffer[i+1]=='$')) {
++i;
wmode=w_fill;
combine.clear();
}
else {
// cmm->debugout << buffer[i];
s_stdout << buffer[i];
if(buffer[i]=='\n') last_output_line.clear();
else last_output_line+=buffer[i];
}
break;
case w_fill:
if(buffer[i]=='$') {
wmode=w_normal;
redraw_line(combine, pos, pos-1);
}
else combine+=buffer[i];
break;
}
}
s_stdout << flush;
}
} while(len>0);
p.clear();
return true;
}
bool receive_stdin(modglue::ipipe& p)
{
char buffer[1024];
int len;
do {
p.read(buffer,1016);
len=p.gcount();
if(len>0)
process_stdin(buffer, len);
} while(len>0);
p.clear();
return true;
}
bool receive_stderr(modglue::ipipe& p)
{
char buffer[1024];
int len;
do {
p.read(buffer,1016);
len=p.gcount();
if(len>0)
s_stderr << buffer << flush;
} while(len>0);
p.clear();
return true;
}
int main(int argc, char **argv)
{
if(argc==1) {
cerr << "prompt";
#ifdef STATICBUILD
cerr << " (static)";
#endif
cerr << "Copyright (C) 2001-2006 Kasper Peeters <kasper.peeters@aei.mpg.de>" << endl << endl
<< "Usage: prompt [program] [args]" << endl;
exit(-1);
}
if(!isatty(0)) {
cerr << "prompt: stdin is not a tty." << endl;
exit(-2);
}
modglue::main mm(argc, argv);
mm.add(&s_stdin, 0);
mm.add(&s_stdout, 1);
mm.add(&s_stderr, 2);
cmm=&mm;
fcntl(0, F_SETFL, O_NONBLOCK);
if(mm.check()) {
int start=1;
string program(argv[start]);
modglue::ext_process proc(program);
for(int i=start+1; i<argc; ++i) {
proc << argv[i];
}
mm.add(&proc);
try {
proc.setup_pipes();
signal(SIGINT,sigc_handler);
proc.fork();
thepid=proc.get_pid();
s_stdin.receiver.connect(sigc::ptr_fun(receive_stdin));
proc.input_pipe("stdout")->receiver.connect(sigc::ptr_fun(receive_stdout));
proc.input_pipe("stderr")->receiver.connect(sigc::ptr_fun(receive_stderr));
t_stdin=proc.output_pipe("stdin");
mm.process_died.connect(sigc::ptr_fun(died));
history.push_back("");
raw_terminal();
mm.run(2);
}
catch(exception& ex) {
cerr << ex.what() << endl;
return -1;
}
s_stdout << std::flush;
tcsetattr(0, TCSANOW, &saved_attributes);
return 0;
}
else return -2;
}
|