tags:

views:

330

answers:

3

Hi everybody,

I'm trying to send an arrow key via the stdin to the bash:

cat | /bin/bash

then i am typing "echo hi" => "hi" appears on the console (of course without the quotes) then i press the arrow key up => ^[[A command not found appears

Is it possible to send an arrow key to an program via the stdin ?

The reason why i am asking is: I want to control the bash from another programm. I would like to send arrow keys to the bash

A: 

Try starting bash with the -i switch.

Teddy
A: 

Now i know how to do this: cat interprets the escape sequence and writes out not the original sequence. dd doesn't interpet the escape sequence. With dd | bash -i, escape sequences can be send to the bash (-i is needed because otherwise the bash doesn't react on cursor keys)

I don't know what system you're running, but that doesn't work at all for me. And the idea that `cat` should alter its data is very foreign to me.
Teddy
+2  A: 

What you really should be doing is create a pseudo-tty device (using openpty() or similar), start bash on that PTY, and send your key strokes through that PTY device. See the section on “Pseudo-Terminals” in the GNU C Library Manual.

Teddy
Yes, then after that make four chars containing the ASCII values of the arrow keys and write them to the master side of the pty with " write( MasterPTY_FD, CharContainingSomeArrowKey, sizeof(CharContainingSomeArrowKey ) " The hex code to assign to the char for the up arrow is 0x18. Down is: 0x19. Left is: 0x1B. Right is: 0x1A
Klaus Fiedler