On 04/01/16 10:27, Pádraig Brady wrote:
> On 04/01/16 08:06, Tomas Krcka wrote:
>> Hi,
>>   I had a problem with setting baudrate of serial line by stty command.
>> I tried to change the baudrate, but the stty went to frozen state.
>> I found out that problem is that kernel has IXON enabled as default
>> and before change of speed the line received DC3 char. It's illegal
>> character in this situation, it happens once per 100 starts of the
>> system.
>>
>> Example situation:
>>    IXON is enabled in kernel by default and a serial line received DC3
>> char and then you can not disable IXON. There is waiting for empty
>> buffer in kernel but sending is stopped because of DC3 char.
>>
>> If TCSADRAIN is changed to TCSANOW then it's correct.
>>
>> Do you have any opinion about the situation ? Is it possible to change
>> TCSADRAIN to TCSANOW or create a new parameter of stty to change the
>> serial line settings immediately ?
> 
> Interesting. I see that Red Hat systems for the last 6 years
> have unconditionally used TCSANOW rather than TCSADRAIN due to:
> https://bugzilla.redhat.com/504798
> 
> I see a similar 13 year old request to debian:
> https://bugs.debian.org/210475
> 
> I see that FreeBSD uses TCSANOW (well the equivalent 0 value).
> 
> Now it's tempting to use TCSANOW unconditionally
> though it does seem safer to drain pending output before changing settings,
> so an option like you suggest seems appropriate.

-I, --immediate option added in the attached

>From ee2a97f06d55a7156f8c07fccb126e41c8947418 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Mon, 4 Jan 2016 12:13:40 +0000
Subject: [PATCH] stty: support -I, --immediate to not wait for pending
 transmission

* src/stty.c (main): Use TCSANOW rather than TCSADRAIN if -I specified.
(usage): Document the new option.
* doc/coreutils.texi (stty invocation): Likewise.
* tests/misc/stty.sh: Ensure -I is supported.
* NEWS: Mention the new feature.
---
 NEWS               |  3 +++
 doc/coreutils.texi |  9 +++++++++
 src/stty.c         | 15 ++++++++++++---
 tests/misc/stty.sh |  3 +++
 4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index 72d69b8..e3ffee6 100644
--- a/NEWS
+++ b/NEWS
@@ -47,6 +47,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   is reusable by most shells, with non-printable characters escaped
   with the POSIX proposed $'...' syntax.
 
+  stty now supports the -i, --immediate option, to apply settings
+  without waiting for transmission of any pending output.
+
 ** Changes in behavior
 
   base64 no longer supports hex or oct --wrap parameters,
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 62955a5..3e6741b 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -13808,6 +13808,15 @@ Print all current settings in a form that can be used as an argument to
 another @command{stty} command to restore the current settings.  This option
 may not be used in combination with any line settings.
 
+@item -I
+@itemx --immediate
+@opindex -I
+@opindex --immediate
+@cindex nonblocking @command{stty} setting
+Apply settings without first waiting for pending output to be transmitted.
+In some cases the serial hardware may be in a state where transmission
+is not possible.
+
 @end table
 
 Many settings can be turned off by preceding them with a @samp{-}.
diff --git a/src/stty.c b/src/stty.c
index 37fbb25..8d79cba 100644
--- a/src/stty.c
+++ b/src/stty.c
@@ -469,6 +469,7 @@ static struct option const longopts[] =
   {"all", no_argument, NULL, 'a'},
   {"save", no_argument, NULL, 'g'},
   {"file", required_argument, NULL, 'F'},
+  {"immediate", no_argument, NULL, 'I'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -522,7 +523,7 @@ usage (int status)
   else
     {
       printf (_("\
-Usage: %s [-F DEVICE | --file=DEVICE] [SETTING]...\n\
+Usage: %s [-F DEVICE | --file=DEVICE] [-I] [SETTING]...\n\
   or:  %s [-F DEVICE | --file=DEVICE] [-a|--all]\n\
   or:  %s [-F DEVICE | --file=DEVICE] [-g|--save]\n\
 "),
@@ -538,6 +539,9 @@ Print or change terminal characteristics.\n\
   -g, --save         print all current settings in a stty-readable form\n\
   -F, --file=DEVICE  open and use the specified DEVICE instead of stdin\n\
 "), stdout);
+      fputs (_("\
+  -i, --immediate    apply setting without waiting for pending transmission\n\
+"), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       fputs (_("\
@@ -1080,6 +1084,7 @@ main (int argc, char **argv)
   bool noargs = true;
   char *file_name = NULL;
   const char *device_name;
+  int tcsetattr_options = TCSADRAIN;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -1103,7 +1108,7 @@ main (int argc, char **argv)
      stty parses options, be sure it still works with combinations of
      short and long options, --, POSIXLY_CORRECT, etc.  */
 
-  while ((optc = getopt_long (argc - argi, argv + argi, "-agF:",
+  while ((optc = getopt_long (argc - argi, argv + argi, "-agF:I",
                               longopts, NULL))
          != -1)
     {
@@ -1125,6 +1130,10 @@ main (int argc, char **argv)
           file_name = optarg;
           break;
 
+        case 'I':
+          tcsetattr_options = TCSANOW;
+          break;
+
         case_GETOPT_HELP_CHAR;
 
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -1361,7 +1370,7 @@ main (int argc, char **argv)
          spurious difference in an uninitialized portion of the structure.  */
       static struct termios new_mode;
 
-      if (tcsetattr (STDIN_FILENO, TCSADRAIN, &mode))
+      if (tcsetattr (STDIN_FILENO, tcsetattr_options, &mode))
         error (EXIT_FAILURE, errno, "%s", quotef (device_name));
 
       /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
diff --git a/tests/misc/stty.sh b/tests/misc/stty.sh
index 666ac0b..88d5796 100755
--- a/tests/misc/stty.sh
+++ b/tests/misc/stty.sh
@@ -35,6 +35,9 @@ stty $(cat $saved_state) || fail=1
 # This would segfault prior to sh-utils-2.0j.
 stty erase - || fail=1
 
+# Ensure --immediate mode is supported
+stty -I erase - || fail=1
+
 # These would improperly ignore invalid options through coreutils 5.2.1.
 returns_ 1 stty -F 2>/dev/null || fail=1
 returns_ 1 stty -raw -F no/such/file 2>/dev/null || fail=1
-- 
2.5.0

Reply via email to