The following issue has been SUBMITTED. 
====================================================================== 
https://austingroupbugs.net/view.php?id=1532 
====================================================================== 
Reported By:                stephane
Assigned To:                
====================================================================== 
Project:                    Issue 8 drafts
Issue ID:                   1532
Category:                   Shell and Utilities
Type:                       Error
Severity:                   Objection
Priority:                   normal
Status:                     New
Name:                       Stephane Chazelas 
Organization:                
User Reference:             https://austingroupbugs.net/view.php?id=1053 
Section:                    stty utility 
Page Number:                 
Line Number:                 
Final Accepted Text:         
====================================================================== 
Date Submitted:             2021-11-03 14:15 UTC
Last Modified:              2021-11-03 14:15 UTC
====================================================================== 
Summary:                    "stty -g" output should not have to be split
Description: 
The resolution of https://austingroupbugs.net/view.php?id=1053, about the
specification of "stty -g"
changes:

-g

    Write to standard output all the current settings in an
    unspecified form that can be used as arguments to another
    invocation of the stty utility on the same system. The form
    used shall not contain any characters that would require
    quoting to avoid word expansion by the shell; see Section
    2.6 (on page 2353).

to:

-g

    Write to standard output all the current settings,
    optionally excluding the terminal window size, in an
    unspecified form that, when used as arguments to another
    invocation of the stty utility on the same system, attempts
    to apply those settings to the terminal. The form used shall
    not contain any sequence that would form an Informational
    Query, nor any characters that would require quoting to
    avoid word expansions, other than field splitting, by the
    shell; see Section 2.6 (on page 2353).

While the 2018 edition had:

-g
    Write to standard output all the current settings in an
    unspecified form that can be used as arguments to another
    invocation of the stty utility on the same system. The form
    used shall not contain any characters that would require
    quoting to avoid word expansion by the shell; see wordexp.

It's one stream of bytes that applications send to standard output.

For that stream of bytes to be "used as argument*S* to another
invocation of the stty utility", it implies that that output should
somehow be split into a list of arguments (and also implies the
result fits in the ARG_MAX limit and doesn't contain NUL bytes).

The reference to 2.6 suggests maybe "sh" should be involved to
perform that splitting. It almost suggests that the output
should be appended to "stty ", or possibly "stty -- " and fed to
sh as in (printf "stty -- "; stty -g) | sh

AFAIK, stty -g was introduced in SysIII circa 1980. It was
outputting on word on one line made of a ":"-separated of alnums
from the portable charset and followed by a newline character.
After it was specified by POSIX, it was also added to BSDs (some
4.3 variant), with "=" in the list of characters that may occur
(not in leading position) in the ":"-delimited list.
GNU stty has used a format similar to SysIII from at least as
far back as 1990.

The way to save and restore it was always to store that output
without the trailing newline characters, which in a shell can be
done with:

saveterm=$(stty -g)

And to restore it:

stty "$saveterm"

The "--" being unnecessary because the output of stty -g never
starts with "-" and with some (including GNU stty) not allowed.

All the documentations of stty I've seen talk of *one* (an)
argument to be passed to stty. They don't say the output may
(let alone should) be split in any way to be passed as several
arguments to stty.

In the unlikely event that there are indeed stty implementations
that require the output of stty -g to be split into arguments
before being fed back to stty, we'd need to specify how those
are meant to be split.

Desired Action: 
Change the description of the -g option to something like:

-g

    Write to standard output all the current settings,
    optionally excluding the terminal window size, in an
    unspecified form that, when stripped of trailing newline
    characters, and used as the one and only argument to another
    invocation of the stty utility on the same system, attempts
    to apply those settings to the terminal. The form used shall
    consist of one line of text consisting of only printable
    characters from the portable character set, excluding
    whitespace characters, NUL, and all characters that are
    special in the syntax of the sh language
    (`"$&*()[]{};'#~?<>\|).

    (alternatively, specify it as the
    ^[[:alnum:]][[:alnum:]=]*:[[:alnum:]=:]*\n
    extended regexp in the C locale.)

A clarifying example would help like:

saveterm=$(stty -g)

and:

[ -n "$saveterm" ] && stty "$saveterm"
[ -n "$saveterm" ] && stty $saveterm
eval "${saveterm:+stty $saveterm}"

Are three ways the settings may be restored, the first one
being the canonical one and the last one assuming none of the
characters allowed in the stty -g output are currently present
in $IFS (and therefore not recommended).

The example should be changed to

saveterm=$(stty −g)                              # save terminal state
restoresize=$(
  stty size | awk '{printf "stty rows %d cols %d", $1, $2}'
)                                                # save terminal size
stty new settings                                # set new state
...
[ -n "$saveterm" ] && stty "$saveterm"           # restore terminal state
eval "$ttysize"                                  # restore terminal size

Also using awk instead of shell IFS-splitting+globbing as as
discussed on the mailing list, $IFS is not guaranteed to contain
all the blank characters in the locale (and anyway yash is the
only shell that considers multi-byte blanks as
IFS-whitespace-characters wrt IFS-splitting), while awk is
required to split fields on blanks (even though not all
implementations do).
====================================================================== 

Issue History 
Date Modified    Username       Field                    Change               
====================================================================== 
2021-11-03 14:15 stephane       New Issue                                    
2021-11-03 14:15 stephane       Name                      => Stephane Chazelas
2021-11-03 14:15 stephane       User Reference            =>
https://austingroupbugs.net/view.php?id=1053      
2021-11-03 14:15 stephane       Section                   => stty utility    
======================================================================


  • [Issue 8 dra... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group
      • Re:... Wayne Pollock via austin-group-l at The Open Group
        • ... Don Cragun via austin-group-l at The Open Group
    • [Issue ... Austin Group Bug Tracker via austin-group-l at The Open Group

Reply via email to