Skip to content

Commit

Permalink
libc:getline support backspace
Browse files Browse the repository at this point in the history
Signed-off-by: zhangwenjian <zhangwenjian@xiaomi.com>
  • Loading branch information
zhangwenjian111 authored and xiaoxiang781216 committed Oct 10, 2024
1 parent de8fae7 commit 9003110
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions system/cle/cle.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <syslog.h>
#include <errno.h>
#include <debug.h>
#include <termios.h>

#include <nuttx/ascii.h>
#include <nuttx/vt100.h>
Expand Down Expand Up @@ -1050,8 +1051,20 @@ int cle_fd(FAR char *line, FAR const char *prompt, uint16_t linelen,
int infd, int outfd)
{
FAR struct cle_s priv;
struct termios cfg;
int ret;

if (isatty(infd))
{
tcgetattr(infd, &cfg);
if (cfg.c_lflag & ICANON)
{
cfg.c_lflag &= ~ICANON;
tcsetattr(infd, TCSANOW, &cfg);
cfg.c_lflag |= ICANON;
}
}

/* Initialize the CLE state structure */

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

if (isatty(infd) && (cfg.c_lflag & ICANON))
{
tcsetattr(infd, TCSANOW, &cfg);
}

return ret;
}

Expand Down
23 changes: 22 additions & 1 deletion system/readline/readline_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <termios.h>

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

struct readline_s vtbl;
struct termios cfg;
ssize_t ret;

if (isatty(infd))
{
tcgetattr(infd, &cfg);
if (cfg.c_lflag & ICANON)
{
cfg.c_lflag &= ~ICANON;
tcsetattr(infd, TCSANOW, &cfg);
cfg.c_lflag |= ICANON;
}
}

/* Set up the vtbl structure */

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

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

return readline_common(&vtbl.vtbl, buf, buflen);
ret = readline_common(&vtbl.vtbl, buf, buflen);

if (isatty(infd) && (cfg.c_lflag & ICANON))
{
tcsetattr(infd, TCSANOW, &cfg);
}

return ret;
}

0 comments on commit 9003110

Please sign in to comment.