↑↑↑ Home ↑↑ UNIX ↑ Updateware

Readline input line support for all

Many programs that allow editing input with the GNU readline library. If you are used to its comfortable and powerful features, those that do not support it are a nuisance. Andrew Tridgell has written a C program that adds readline features to another program's standard input. Though I got it to compile with minor modifications, it worked less than perfectly, often producing errors.

Before I got round to trying to fix it, I hit on a much easier way. The networking relay socat supports readline input editing, and it is versatile enough to connect a readline-enhanced input line to an otherwise stupid shell program. Just define the following shell function:

rline () 
{ 
    socat READLINE EXEC:"$*",pty
}

You can then add readline input features to a command by prepending it with the rline function:

rline command arguments ...

Here is a brief explanation: socat always connects two endpoints, usually (as here) with a duplex (bidirectional) connection. In this case the first endpoint is I/O to the terminal, with the input enhanced by the readline library. The other endpoint is the standard input and standard output of the command line that is given in the arguments of rline. The pty option is important, as it tricks the program into thinking that it is running in a terminal.

A few drawbacks and limitations should be mentioned. The readline editing occurs without the program knowing about it; the input line gets forwarded to its standard input only when you press the Return key. This entails that the program prints the terminal echo of the input line a second time. Since socat doesn't know anything of the meaning of the input, it also echos anything on the terminal, including passwords that the program would not. And using the shell function above, it is not possible to run programs with arguments containing spaces, which will get broken up.

Update: Since writing this page, I have learnt about rlwrap which is also a readline front-end for command-line programs. It is much more powerful and versatile than rline, for example in that it supports a command history and treats passwords specially. rline still has the edge in some cases – for example, it manages to close programs on Ctrl-D which do not support that (by closing their stdin). But you will want to try rlwrap first if available.