new_perl
new_perl

Reputation: 7735

How do I implement a console with ssh features in C?

That is,it should support up/down arrow keys and ctrl-r to reverse searching. WHere should I start?

Upvotes: 3

Views: 413

Answers (1)

evil otto
evil otto

Reputation: 10582

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.

Upvotes: 1

Related Questions