Skip to content

Commit 9003110

Browse files
zhangwenjian111xiaoxiang781216
authored andcommitted
libc:getline support backspace
Signed-off-by: zhangwenjian <zhangwenjian@xiaomi.com>
1 parent de8fae7 commit 9003110

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

system/cle/cle.c

+18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <syslog.h>
3636
#include <errno.h>
3737
#include <debug.h>
38+
#include <termios.h>
3839

3940
#include <nuttx/ascii.h>
4041
#include <nuttx/vt100.h>
@@ -1050,8 +1051,20 @@ int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
10501051
int infd, int outfd)
10511052
{
10521053
FAR struct cle_s priv;
1054+
struct termios cfg;
10531055
int ret;
10541056

1057+
if (isatty(infd))
1058+
{
1059+
tcgetattr(infd, &cfg);
1060+
if (cfg.c_lflag & ICANON)
1061+
{
1062+
cfg.c_lflag &= ~ICANON;
1063+
tcsetattr(infd, TCSANOW, &cfg);
1064+
cfg.c_lflag |= ICANON;
1065+
}
1066+
}
1067+
10551068
/* Initialize the CLE state structure */
10561069

10571070
memset(&priv, 0, sizeof(struct cle_s));
@@ -1116,6 +1129,11 @@ int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
11161129
}
11171130
#endif /* CONFIG_SYSTEM_CLE_CMD_HISTORY */
11181131

1132+
if (isatty(infd) && (cfg.c_lflag & ICANON))
1133+
{
1134+
tcsetattr(infd, TCSANOW, &cfg);
1135+
}
1136+
11191137
return ret;
11201138
}
11211139

system/readline/readline_fd.c

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <unistd.h>
2929
#include <errno.h>
3030
#include <assert.h>
31+
#include <termios.h>
3132

3233
#include "system/readline.h"
3334
#include "readline.h"
@@ -205,6 +206,19 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd)
205206
UNUSED(outfd);
206207

207208
struct readline_s vtbl;
209+
struct termios cfg;
210+
ssize_t ret;
211+
212+
if (isatty(infd))
213+
{
214+
tcgetattr(infd, &cfg);
215+
if (cfg.c_lflag & ICANON)
216+
{
217+
cfg.c_lflag &= ~ICANON;
218+
tcsetattr(infd, TCSANOW, &cfg);
219+
cfg.c_lflag |= ICANON;
220+
}
221+
}
208222

209223
/* Set up the vtbl structure */
210224

@@ -219,5 +233,12 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd)
219233

220234
/* The let the common readline logic do the work */
221235

222-
return readline_common(&vtbl.vtbl, buf, buflen);
236+
ret = readline_common(&vtbl.vtbl, buf, buflen);
237+
238+
if (isatty(infd) && (cfg.c_lflag & ICANON))
239+
{
240+
tcsetattr(infd, TCSANOW, &cfg);
241+
}
242+
243+
return ret;
223244
}

0 commit comments

Comments
 (0)