Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventing arrow keys's outputing raw characters like ^[[A when making a shell in C

Tags:

c

shell

I am implementing a shell-like program where user types command (defined by me). Just like this.

>cmd
result blah blah blah
>

When I use arrow keys it outputs raw characters like ^[[A.

>^[[A

I also notice sqlite3 behaves like this at least in the version I compiled on my computer.

How to prevent this and let <- and -> keys move cursor left and right?

like image 886
onemach Avatar asked Jan 17 '23 23:01

onemach


1 Answers

GNU Readline is a library specifically designed for this task (that is, allowing the user to edit commands typed at an interactive command-driven program). Note that this library is distributed under the GPL (not the LGPL); if that won't work for you, editline is a similar library with a BSD-style license.

I note that you say this is homework, so you might want to ask your instructor whether you are expected to implement cursor motion and line editing yourself. If so, ncurses (as mentioned by jDourlens) is the next step down in terms of abstraction, and if you really want to do everything yourself, read up on termios and the VT-220 control sequences (nearly all terminal emulators used nowadays emulate the VT220 or one of its descendants).

like image 115
zwol Avatar answered Jan 31 '23 07:01

zwol