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

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

commit b60cdf4927a339f64def9dc5902b8091934e0ee6
Author: Xiang Xiao <[email protected]>
AuthorDate: Mon Sep 4 02:31:17 2023 +0800

    system/cu: Skip the terminal related stuff if dev isn't a tty
    
    Signed-off-by: Xiang Xiao <[email protected]>
---
 system/cu/cu_main.c | 98 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 52 insertions(+), 46 deletions(-)

diff --git a/system/cu/cu_main.c b/system/cu/cu_main.c
index ea0df68aa..cf74f0815 100644
--- a/system/cu/cu_main.c
+++ b/system/cu/cu_main.c
@@ -131,57 +131,58 @@ static int set_termios(FAR struct cu_globals_s *cu, int 
rate,
 static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
 #endif
 {
-  int rc = 0;
   int ret;
   struct termios tio;
 
-  tio = cu->devtio;
+  if (isatty(cu->devfd))
+    {
+      tio = cu->devtio;
 
 #ifdef CONFIG_SERIAL_TERMIOS
-  tio.c_cflag &= ~(PARENB | PARODD | CRTSCTS);
+      tio.c_cflag &= ~(PARENB | PARODD | CRTSCTS);
 
-  switch (parity)
-    {
-      case PARITY_EVEN:
-        tio.c_cflag |= PARENB;
-        break;
+      switch (parity)
+        {
+          case PARITY_EVEN:
+            tio.c_cflag |= PARENB;
+            break;
 
-      case PARITY_ODD:
-        tio.c_cflag |= PARENB | PARODD;
-        break;
+          case PARITY_ODD:
+            tio.c_cflag |= PARENB | PARODD;
+            break;
 
-      case PARITY_NONE:
-        break;
-    }
+          case PARITY_NONE:
+            break;
+        }
 
-  /* set baudrate */
+      /* Set baudrate */
 
-  if (rate != 0)
-    {
-      cfsetspeed(&tio, rate);
-    }
+      if (rate != 0)
+        {
+          cfsetspeed(&tio, rate);
+        }
 
-  if (rtscts)
-    {
-      tio.c_cflag |= CRTS_IFLOW | CCTS_OFLOW;
-    }
+      if (rtscts)
+        {
+          tio.c_cflag |= CRTS_IFLOW | CCTS_OFLOW;
+        }
 #endif
 
-  tio.c_oflag = OPOST;
+      tio.c_oflag = OPOST;
 
-  /* enable or disable \n -> \r\n conversion during write */
+      /* Enable or disable \n -> \r\n conversion during write */
 
-  if (nocrlf == 0)
-    {
-      tio.c_oflag |= ONLCR;
-    }
+      if (nocrlf == 0)
+        {
+          tio.c_oflag |= ONLCR;
+        }
 
-  ret = tcsetattr(cu->devfd, TCSANOW, &tio);
-  if (ret)
-    {
-      cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
-      rc = -1;
-      goto errout;
+      ret = tcsetattr(cu->devfd, TCSANOW, &tio);
+      if (ret)
+        {
+          cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
+          return ret;
+        }
     }
 
   /* Let the remote machine to handle all crlf/echo except Ctrl-C */
@@ -197,19 +198,21 @@ static int set_termios(FAR struct cu_globals_s *cu, int 
nocrlf)
     ret = tcsetattr(cu->stdfd, TCSANOW, &tio);
     if (ret)
       {
-        cu_error("set_termios: ERROR during tcsetattr(): %d\n",
-                errno);
-        rc = -1;
+        cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
+        return ret;
       }
   }
 
-errout:
-  return rc;
+  return 0;
 }
 
 static void retrieve_termios(FAR struct cu_globals_s *cu)
 {
-  tcsetattr(cu->devfd, TCSANOW, &cu->devtio);
+  if (isatty(cu->devfd))
+    {
+      tcsetattr(cu->devfd, TCSANOW, &cu->devtio);
+    }
+
   if (cu->stdfd >= 0)
     {
       tcsetattr(cu->stdfd, TCSANOW, &cu->stdtio);
@@ -361,17 +364,20 @@ int main(int argc, FAR char *argv[])
   if (cu->devfd < 0)
     {
       cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n",
-              devname, errno);
+               devname, errno);
       goto errout_with_devinit;
     }
 
   /* Remember serial device termios attributes */
 
-  ret = tcgetattr(cu->devfd, &cu->devtio);
-  if (ret)
+  if (isatty(cu->devfd))
     {
-      cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
-      goto errout_with_devfd;
+      ret = tcgetattr(cu->devfd, &cu->devtio);
+      if (ret)
+        {
+          cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
+          goto errout_with_devfd;
+        }
     }
 
   /* Remember std termios attributes if it is a tty. Try to select

Reply via email to