Enable/disable all postprocessing instead of just nl->crnl processing

since things like tab expansion matter too.  However, if stdout is
a tty leave postprocessing on in the pty since we run into problems
doing it only on the real stdout with .e.g nvi.
This commit is contained in:
Todd C. Miller
2009-12-23 23:53:04 +00:00
parent ce63ab5069
commit 7a759cd2ac
2 changed files with 21 additions and 18 deletions

View File

@@ -103,6 +103,7 @@ struct script_buf {
}; };
static int script_fds[6]; static int script_fds[6];
static int ttyout;
static sig_atomic_t alive = 1; static sig_atomic_t alive = 1;
static sig_atomic_t recvsig = 0; static sig_atomic_t recvsig = 0;
@@ -361,7 +362,7 @@ check_foreground()
{ {
foreground = tcgetpgrp(script_fds[SFD_USERTTY]) == parent; foreground = tcgetpgrp(script_fds[SFD_USERTTY]) == parent;
if (foreground && !tty_initialized) { if (foreground && !tty_initialized) {
if (term_copy(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE], 0)) { if (term_copy(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE], ttyout)) {
tty_initialized = 1; tty_initialized = 1;
sync_winsize(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE]); sync_winsize(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE]);
} }
@@ -396,7 +397,7 @@ suspend_parent(signo, output, then, now, ofile, tfile)
if (foreground) { if (foreground) {
if (ttymode != TERM_RAW) { if (ttymode != TERM_RAW) {
do { do {
n = term_raw(script_fds[SFD_USERTTY], 1, 0); n = term_raw(script_fds[SFD_USERTTY], !ttyout, 0);
} while (!n && errno == EINTR); } while (!n && errno == EINTR);
ttymode = TERM_RAW; ttymode = TERM_RAW;
} }
@@ -440,7 +441,7 @@ suspend_parent(signo, output, then, now, ofile, tfile)
if (foreground) { if (foreground) {
/* Set raw/cbreak mode. */ /* Set raw/cbreak mode. */
do { do {
n = term_raw(script_fds[SFD_USERTTY], 1, n = term_raw(script_fds[SFD_USERTTY], !ttyout,
ttymode == TERM_CBREAK); ttymode == TERM_CBREAK);
} while (!n && errno == EINTR); } while (!n && errno == EINTR);
} else { } else {
@@ -538,6 +539,9 @@ script_execv(path, argv)
sigaction(SIGTTOU, &sa, NULL); sigaction(SIGTTOU, &sa, NULL);
#endif #endif
/* If stdout is not a tty we handle post-processing differently. */
ttyout = isatty(STDOUT_FILENO);
/* /*
* We communicate with the child over a bi-directional pipe. * We communicate with the child over a bi-directional pipe.
* Parent sends signal info to child and child sends back wait status. * Parent sends signal info to child and child sends back wait status.
@@ -547,15 +551,16 @@ script_execv(path, argv)
if (foreground) { if (foreground) {
/* Copy terminal attrs from user tty -> pty slave. */ /* Copy terminal attrs from user tty -> pty slave. */
if (term_copy(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE], 0)) { if (term_copy(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE], ttyout)) {
tty_initialized = 1; tty_initialized = 1;
sync_winsize(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE]); sync_winsize(script_fds[SFD_USERTTY], script_fds[SFD_SLAVE]);
} }
/* Start out in raw mode is stdout is a tty. */ /* Start out in raw mode is stdout is a tty. */
ttymode = isatty(STDOUT_FILENO) ? TERM_RAW : TERM_CBREAK; ttymode = ttyout ? TERM_RAW : TERM_CBREAK;
do { do {
n = term_raw(script_fds[SFD_USERTTY], 1, ttymode == TERM_CBREAK); n = term_raw(script_fds[SFD_USERTTY], !ttyout,
ttymode == TERM_CBREAK);
} while (!n && errno == EINTR); } while (!n && errno == EINTR);
if (!n) if (!n)
log_error(USE_ERRNO, "Can't set terminal to raw mode"); log_error(USE_ERRNO, "Can't set terminal to raw mode");
@@ -875,7 +880,7 @@ script_child(path, argv, backchannel, rbac_enabled)
close(n); close(n);
#endif #endif
if (foreground && !isatty(STDOUT_FILENO)) if (foreground && !ttyout)
foreground = 0; foreground = 0;
/* Start command and wait for it to stop or exit */ /* Start command and wait for it to stop or exit */

20
term.c
View File

@@ -144,9 +144,9 @@ term_noecho(fd)
#if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H) #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
int int
term_raw(fd, onlcr, isig) term_raw(fd, opost, isig)
int fd; int fd;
int onlcr; int opost;
int isig; int isig;
{ {
struct termios term; struct termios term;
@@ -156,10 +156,8 @@ term_raw(fd, onlcr, isig)
(void) memcpy(&term, &oterm, sizeof(term)); (void) memcpy(&term, &oterm, sizeof(term));
/* Set terminal to raw mode */ /* Set terminal to raw mode */
term.c_iflag &= ~(ICRNL|IGNCR|INLCR|IUCLC|IXON); term.c_iflag &= ~(ICRNL|IGNCR|INLCR|IUCLC|IXON);
/* Retain NL to NLCR conversion if onlcr flag set. */ /* Only retain output post-processing opost flag set. */
if (onlcr && ISSET(term.c_oflag, ONLCR|OPOST)) if (!opost)
term.c_oflag = ONLCR|OPOST;
else
term.c_oflag &= ~OPOST; term.c_oflag &= ~OPOST;
term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
if (isig) if (isig)
@@ -198,18 +196,18 @@ term_cbreak(fd)
} }
int int
term_copy(src, dst, onlcr) term_copy(src, dst, opost)
int src; int src;
int dst; int dst;
int onlcr; int opost;
{ {
struct termios tt; struct termios tt;
if (tcgetattr(src, &tt) != 0) if (tcgetattr(src, &tt) != 0)
return(0); return(0);
/* Do not convert line endings from NL to NLCR. */ /* Do not do post-processing unless opost set. */
if (!onlcr) if (!opost)
CLR(tt.c_oflag, ONLCR); CLR(tt.c_oflag, OPOST);
/* XXX - add TCSANOW compat define */ /* XXX - add TCSANOW compat define */
if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0) if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
return(0); return(0);