This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 9003110d7 libc:getline support backspace
9003110d7 is described below

commit 9003110d7f68a0b4c24519b24a3254b6a23446e5
Author: zhangwenjian <[email protected]>
AuthorDate: Thu May 23 13:17:20 2024 +0800

    libc:getline support backspace
    
    Signed-off-by: zhangwenjian <[email protected]>
---
 system/cle/cle.c              | 18 ++++++++++++++++++
 system/readline/readline_fd.c | 23 ++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/system/cle/cle.c b/system/cle/cle.c
index 9a0c0119c..06bdb0b53 100644
--- a/system/cle/cle.c
+++ b/system/cle/cle.c
@@ -35,6 +35,7 @@
 #include <syslog.h>
 #include <errno.h>
 #include <debug.h>
+#include <termios.h>
 
 #include <nuttx/ascii.h>
 #include <nuttx/vt100.h>
@@ -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));
@@ -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;
 }
 
diff --git a/system/readline/readline_fd.c b/system/readline/readline_fd.c
index 351b416e4..914cafdde 100644
--- a/system/readline/readline_fd.c
+++ b/system/readline/readline_fd.c
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <assert.h>
+#include <termios.h>
 
 #include "system/readline.h"
 #include "readline.h"
@@ -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 */
 
@@ -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;
 }

Reply via email to