That is,it should support up/down arrow keys and ctrl-r to reverse searching. WHere should I start?
-
Do you want all of SSH features, or just the ability to have command history?Alan– Alan2011-08-02 01:17:23 +00:00Commented Aug 2, 2011 at 1:17
-
2If you mean that you want the nice line-editing features, investigate the GNU readline library, which supplies these features to many of the programs you see them in.Jeremy Roman– Jeremy Roman2011-08-02 01:19:30 +00:00Commented Aug 2, 2011 at 1:19
-
those items are not ssh features, they are features of the shell (bash/tcsh/zsh), which likely uses readline to implement them.evil otto– evil otto2011-08-02 01:20:41 +00:00Commented Aug 2, 2011 at 1:20
-
@Alan ,I only need those mentioned features.new_perl– new_perl2011-08-02 01:26:36 +00:00Commented Aug 2, 2011 at 1:26
1 Answer
The answer depends on how low-level you want to be. using readline or editline are the high-level answers. Next level down would be to use libncurses and call getch() to read keyboard input, and then handle the history/searching yourself.
Lowest level (for a terminal) is handling the actual input stream of bytes. The arrow keys send particular character sequences depending on your particular terminal. For example, a vt100 emulator will send ^[[A for "up-arrow", ^[[B for "down-arrow", and so forth. To read these, you'll need to set your terminal attributes to return input immediately and not wait for a newline; to do this use termios functions to disable canonical input mode. Then just read input a character at a time, and see if you get characters (27, 91, 65) and you know that's an up-arrow and respond accordingly.
This low level is tedious and fragile and won't work if you use a different terminal emulator (although you could use terminfo to get the appropriate input sequences for other terminals.)
If you're at a lower level than a terminal (serial line, X window, bitmap display, whatever), then the answers change again.