Skip to content
Snippets Groups Projects
Commit ac77cc33 authored by Fredrik Hübinette (Hubbe)'s avatar Fredrik Hübinette (Hubbe)
Browse files

support for readline, history and ^C

Rev: NT/tools/lib.pike:1.12
parent ea14d99d
Branches
Tags
No related merge requests found
...@@ -180,8 +180,65 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent) ...@@ -180,8 +180,65 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent)
{ {
werror("Failed to connect "+strerror(errno())+".\n"); werror("Failed to connect "+strerror(errno())+".\n");
exit(1); exit(1);
}
class SimpleInOut
{
object o=Stdio.File("stdin");
string read() {
string s=o->read(1000,1);
if(!strlen(s)) return 0;
return s;
} }
int write(string s)
{
return Stdio.stdout->write(s);
}
};
object inout;
#if __VERSION__ > 0.699999 && constant(thread_create)
class RLInOut
{
Thread.Mutex m=Thread.Mutex();
object rl=Stdio.Readline();
string prompt="";
string read()
{
string tmp=rl->read();
if(tmp)
{
tmp+="\n";
prompt="";
}
return tmp;
}
int write(string s)
{
s=prompt+s-"\r";
array lines=s/"\n";
rl->write(lines[..sizeof(s)-2]*"\n");
rl->set_prompt(prompt=lines[-1]);
return strlen(s);
}
void create()
{
rl->enable_history(512);
}
};
if(!silent && !!Stdio.stdin->tcgetattr())
{
inout=RLInOut();
}else
#endif
inout=SimpleInOut();
f->write(sprintf("%4c",sizeof(cmd))); f->write(sprintf("%4c",sizeof(cmd)));
for(int e=0;e<sizeof(cmd);e++) for(int e=0;e<sizeof(cmd);e++)
f->write(sprintf("%4c%s",strlen(cmd[e]),cmd[e])); f->write(sprintf("%4c%s",strlen(cmd[e]),cmd[e]));
...@@ -192,12 +249,26 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent) ...@@ -192,12 +249,26 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent)
// werror("FNORD\n"); // werror("FNORD\n");
thread_create(lambda(object f) thread_create(lambda(object f)
{ {
object stdin=Stdio.File("stdin"); int killed;
while(string s=stdin->read(1000,1)) void killme()
{
if(!killed)
{
werror("\r\n** Interupted\r\n");
f->write_oob("x");
killed=1;
}
};
if(f->write_oob)
{ {
if(!strlen(s)) break; signal(signum("SIGINT"), killme);
f->write(s);
} }
while(string s=inout->read())
f->write(s);
signal(signum("SIGINT"), 0);
f->close("w"); f->close("w");
},f); },f);
...@@ -212,8 +283,8 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent) ...@@ -212,8 +283,8 @@ int silent_do_cmd(string *cmd, mixed|void filter, int|void silent)
if(!len) break; if(!len) break;
s=f->read(len); s=f->read(len);
s=replace(s,"\r\n","\n"); s=replace(s,"\r\n","\n");
if(!silent) write(s); if(!silent) inout->write(s);
ret+=s; if(filter) ret+=s;
} }
if(filter) filter(ret); if(filter) filter(ret);
sscanf(f->read(4),"%4c",int code); sscanf(f->read(4),"%4c",int code);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment