zsh-workers
 help / color / mirror / code / Atom feed
* When is STTY ignored?
@ 2017-05-29 20:36 Stephane Chazelas
  2017-05-30  0:02 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2017-05-29 20:36 UTC (permalink / raw)
  To: Zsh hackers list

Hello,

It's not very clear to me when one may use STTY.

STTY=-echo line
zsh -c 'STTY=-echo line'
STTY=-echo command line
f() line; STTY=-echo f
f() STTY=-echo line; f

work (at reading a line with tty echo disabled)

But:

STTY=-echo read line
STTY=-echo eval line
zsh -c 'f() line; STTY=-echo f'
zsh -c 'f() STTY=-echo line; f'

don't (stty not run).

See also:

$ f() STTY=-onlcr line; STTY=-echo f; stty
asd
   asd
      speed 38400 baud; line = 0;
                                 -brkint -imaxbel iutf8
                                                       -onlcr

-echo not applied, -onlcr not restored.

-- 
Stephane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When is STTY ignored?
  2017-05-29 20:36 When is STTY ignored? Stephane Chazelas
@ 2017-05-30  0:02 ` Bart Schaefer
  2017-05-30  6:53   ` Stephane Chazelas
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2017-05-30  0:02 UTC (permalink / raw)
  To: Zsh hackers list

On May 29,  9:36pm, Stephane Chazelas wrote:
}
} It's not very clear to me when one may use STTY.

It applies only to external commands, not those executed within zsh
(functions, loops, other multiline constructs, builtins).

If it's in the environment of a function et al., it'll apply to all
external commands run from within that construct.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When is STTY ignored?
  2017-05-30  0:02 ` Bart Schaefer
@ 2017-05-30  6:53   ` Stephane Chazelas
  2017-05-31  6:27     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2017-05-30  6:53 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

2017-05-29 17:02:09 -0700, Bart Schaefer:
> On May 29,  9:36pm, Stephane Chazelas wrote:
> }
> } It's not very clear to me when one may use STTY.
> 
> It applies only to external commands, not those executed within zsh
> (functions, loops, other multiline constructs, builtins).
> 
> If it's in the environment of a function et al., it'll apply to all
> external commands run from within that construct.

Thanks. That doesn't seem to be the whole story though.

See the examples I gave

"line" is an external command. "eval" and "command" are builtin
commands.

STTY=-echo line
STTY=-echo command line
f() line; STTY=-echo f

Do work, but not

STTY=-echo eval line

About zsh -c, it looks like it "works" as long as there's only one
command:

zsh -c 'STTY=-echo line'

works (well, it doesn't seem to restore the settings afterwards)
but not

zsh -c 'STTY=-echo line; exit'

It seems it never works in scripts even if they contain a single
command. With scripts source from interactive shells, they seem
to "work" but don't restore the tty settings.

See also:

line | STTY=-echo wc -c
STTY=-echo head -n1 /dev/tty < /dev/null

which don't "work" (even if they called stty, stty would fail
because stdin is not a terminal anyway, but that would still be
worth documenting).

In:

(STTY=-echo line; STTY=-echo line); line

the "-echo" is only applied to the 2nd and 3rd "line"
invocations (instead of 1st and 2nd).

And the issue when several are stacked mentioned before:

$ f() STTY=-onlcr line; STTY=-echo f; stty
asd
   asd
      speed 38400 baud; line = 0;
                                 -brkint -imaxbel iutf8
                                                       -onlcr

-echo not applied, -onlcr not restored.

It would be worth documenting that it can only be used in
interactive shells and only for the simplest case of one simple
external command at the prompt, not in subshell, and without
stdin redirection

-- 
Stephane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: When is STTY ignored?
  2017-05-30  6:53   ` Stephane Chazelas
@ 2017-05-31  6:27     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2017-05-31  6:27 UTC (permalink / raw)
  To: Zsh hackers list

On May 30,  7:53am, Stephane Chazelas wrote:
}
} In:
} 
} (STTY=-echo line; STTY=-echo line); line
} 
} the "-echo" is only applied to the 2nd and 3rd "line"
} invocations (instead of 1st and 2nd).

So, this is a little odd.

The "stty" command is run only when isatty(0) and the process is the
group leader, i.e., the foreground job.

In the construct
    (command1; command2)
the group leader is the subshell, so STTY is ignored for command1;
but then command2 is optimized to an "exec" so it assumes the group
leadership and "stty" is run.  However, that exec means the subshell
is gone and there is never an opportunity to undo the setting change,
so it remains in effect after command2 exits, and that's why it
appears to have been applied to the 3rd command in your example.

This means we ought to treat having STTY in the environment as a
reason to skip the optimization.  However, I'm not sure we look for
STTY early enough in the call chain to handle this.  For example,
jobtab[thisjob].stty_in_env is only assigned after the decision is
made and the child has been forked.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-05-31  6:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-29 20:36 When is STTY ignored? Stephane Chazelas
2017-05-30  0:02 ` Bart Schaefer
2017-05-30  6:53   ` Stephane Chazelas
2017-05-31  6:27     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).