www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mimicking a shell

reply Jan <jan allersma.be> writes:
Hi,

Is there a way to forward all input and output from a shell? This 
implies that e.g. pressing the left arrow on the keyboard is 
immediately being forwarded to the shell and that the output from 
a shell would be *exactly* the same as output from my D program 
(that would include the prompt and VGA colouring).

Kind regards,
Jan
Dec 29 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote:
 Is there a way to forward all input and output from a shell?
yes, but it is platform specific and can be a decent amount of code. what OS are you on?
Dec 29 2019
parent reply Jan <jan allersma.be> writes:
On Sunday, 29 December 2019 at 19:21:53 UTC, Adam D. Ruppe wrote:
 On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote:
 Is there a way to forward all input and output from a shell?
yes, but it is platform specific and can be a decent amount of code. what OS are you on?
I am using Linux (Fedora). Thanks, angel. I'll take a look at your suggestion :)
Jan 04 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 4 January 2020 at 18:43:13 UTC, Jan wrote:
 I am using Linux (Fedora).
ok, the starting point is `openpty` which gives you a communication pipe that the other program sees as a terminal. from there if you are just forwarding you can perhaps shoot bytes to and from without interpreting them. but if you want to send like a synthetic arrow keystroke, well, things get ugly again, it will need to send the right series of bytes based on what terminal the program thinks you are. Like "\033[C" is right arrow on xterm, probably a reasonably safe bet you can make that work. hit ctrl+v then a key in your terminal to see the output. ^[ == "\033" btw there.
 Thanks,  angel. I'll take a look at your suggestion :)
yes good luck!
Jan 04 2020
parent Jan <jan allersma.be> writes:
I think the suggestion of angel would be most fitting for my 
case. As angel said, the using the C code for D would be a 
relatively small refactor.

 if you want to send like a synthetic arrow keystroke, well, 
 things get ugly again, it will need to send the right series of 
 bytes based on what terminal the program thinks you are.
Using arrow key etc. would be very beneficial for my case. So I thank Adam for his solution, but I prefer angel's one. Thanks guys :)
Jan 06 2020
prev sibling parent angel <andrey.gelman gmail.com> writes:
On Sunday, 29 December 2019 at 17:03:14 UTC, Jan wrote:
 Hi,

 Is there a way to forward all input and output from a shell? 
 This implies that e.g. pressing the left arrow on the keyboard 
 is immediately being forwarded to the shell and that the output 
 from a shell would be *exactly* the same as output from my D 
 program (that would include the prompt and VGA colouring).

 Kind regards,
 Jan
IMHO, this is kinda what you need: https://github.com/greenduck/shell-tunnel/blob/master/shell-tunnel.c Sorry, this code is in C, but converting it to D would be fairly straight forward.
Dec 29 2019