zsh-workers
 help / color / Atom feed
* support for POSIX job control terminal management in zsh
@ 2020-06-23  1:39 Godmar Back
  2020-06-23  1:53 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Godmar Back @ 2020-06-23  1:39 UTC (permalink / raw)
  To: zsh-workers

Hi,

I noticed that zsh does not implement POSIX.1 when it comes to
saving/restoring the terminal state of processes suspended with
SIGTSTP.  (The same applies to bash, btw, but not to ksh.)  Shells
like tcsh or ash/dash also do not implement it.

POSIX.1 asks that:

"When a foreground (not background) job stops, the shell must sample
and remember the current terminal settings so that it can restore them
later when it continues the stopped job in the foreground (via the
tcgetattr( ) and tcsetattr( ) functions)."

Consequently, a program such as the one attached below should not fail
any assertions.
However, it does not appear to work when run under zsh.

I've had an illuminating discussion with Chet Ramey, the author of
bash, regarding bash's behavior. He pointed out that applications
historically could never rely on the shell provided job control in a
manner that they could be oblivious to being suspended/resumed, and
therefore always had to handle the saving/restoring of the terminal
state upon suspend/restore themselves, rather than relying on the job
control shell as POSIX asks.

Still, at least one shell (ksh) does appear to implement it.

Hence my question, out of curiosity, is: why does zsh not follow
POSIX.1 semantics in this regard?

Thanks.

 - Godmar

// ts_test.c
/* Test that terminal state is properly restored when a process is
stopped and restored. */
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <signal.h>

#define CTRL_D  4
#define CTRL_E  5

int
main()
{
    int terminal_fd = open(ctermid(NULL), O_RDWR);
    assert (terminal_fd != -1);

    // Step 1. make a change to the terminal state:
    // change VEOF from Ctrl-D to Ctrl-E
    struct termios saved_tty_state;
    int rc = tcgetattr(terminal_fd, &saved_tty_state);
    assert (rc == 0);

    assert (saved_tty_state.c_cc[VEOF] == CTRL_D);       // ^D
    saved_tty_state.c_cc[VEOF] = CTRL_E;                 // ^E
    rc = tcsetattr(terminal_fd, TCSANOW, &saved_tty_state);
    assert (rc == 0);

    // Step 2.  Suspend and let user resume
    printf("This job should now stop, please run 'fg' to continue it\n");
    raise(SIGTSTP);
    printf("Job now continuing...\n");

    // Step 3.
    // Expect that job control shell saved the terminal state
    rc = tcgetattr(terminal_fd, &saved_tty_state);
    assert (rc == 0);
    if (saved_tty_state.c_cc[VEOF] != CTRL_E) {
        printf("I expected a POSIX job control shell to preserve my
terminal settings\n");
        printf("VEOF was not saved, it is %d...\n", saved_tty_state.c_cc[VEOF]);
    }

    assert (saved_tty_state.c_cc[VEOF] == CTRL_E);       // ^E
}

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

* Re: support for POSIX job control terminal management in zsh
  2020-06-23  1:39 support for POSIX job control terminal management in zsh Godmar Back
@ 2020-06-23  1:53 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2020-06-23  1:53 UTC (permalink / raw)
  To: Godmar Back; +Cc: zsh-workers

On Mon, Jun 22, 2020 at 6:40 PM Godmar Back <godmar@gmail.com> wrote:
>
> Hence my question, out of curiosity, is: why does zsh not follow
> POSIX.1 semantics in this regard?

The direct answer is because zsh didn't even begin to claim any level
of POSIX compliance until those constraints began to be added to the
"sh" emulation mode, more than 10 years after the shell was written.

The indirect answer is because this is a pretty dark corner case, and
there is no organized effort to examine the entire POSIX spec for
items that haven't yet been included.  What effort there has been has
been focused on shell scripting more than interactive use.

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23  1:39 support for POSIX job control terminal management in zsh Godmar Back
2020-06-23  1:53 ` Bart Schaefer

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git