From fe9fc5ad1b0f4eab2b9ae3d4a50f622b6048ec2e Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Wed, 30 Sep 2009 02:12:35 +0000 Subject: [PATCH] Retain NL to NLCR conversion on the real tty and skip it on the pty we allocate. That way, if stdout is not a pty there are no extra carriage returns. --- script.c | 4 ++-- sudo.h | 4 ++-- term.c | 31 ++++++++++++++++++++++++------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/script.c b/script.c index 30fc3adc9..6440d6dab 100644 --- a/script.c +++ b/script.c @@ -237,12 +237,12 @@ script_setup() log_error(USE_ERRNO, "Can't get pty"); /* Copy terminal attrs from stdin -> pty slave. */ - if (!term_copy(STDIN_FILENO, script_fds[SFD_SLAVE])) { + if (!term_copy(STDIN_FILENO, script_fds[SFD_SLAVE], 0)) { log_error(USE_ERRNO, "Can't copy terminal attributes"); } sync_winsize(STDIN_FILENO, script_fds[SFD_SLAVE]); - if (!term_raw(STDIN_FILENO)) + if (!term_raw(STDIN_FILENO, 1)) log_error(USE_ERRNO, "Can't set terminal to raw mode"); /* diff --git a/sudo.h b/sudo.h index 0843abbd2..0db98fa47 100644 --- a/sudo.h +++ b/sudo.h @@ -332,9 +332,9 @@ int script_execv __P((char *, char **)); void script_nextid __P((void)); void script_setup __P((void)); int term_cbreak __P((int)); -int term_copy __P((int, int)); +int term_copy __P((int, int, int)); int term_noecho __P((int)); -int term_raw __P((int)); +int term_raw __P((int, int)); int term_restore __P((int)); char *get_timestr __P((time_t, int)); YY_DECL; diff --git a/term.c b/term.c index 92f381291..9811673c1 100644 --- a/term.c +++ b/term.c @@ -137,8 +137,9 @@ term_noecho(fd) #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H) int -term_raw(fd) +term_raw(fd, onlcr) int fd; + int onlcr; { struct termios term; @@ -147,11 +148,15 @@ term_raw(fd) (void) memcpy(&term, &oterm, sizeof(term)); /* Set terminal to raw mode */ term.c_iflag &= ~(ICRNL|IGNCR|INLCR|IUCLC|IXON); - term.c_oflag &= ~OPOST; + /* Retain NL to NLCR conversion if onlcr flag set. */ + if (onlcr && ISSET(term.c_oflag, ONLCR|OPOST)) + term.c_oflag = ONLCR|OPOST; + else + term.c_oflag &= ~OPOST; term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term) == 0) { + if (tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &term) == 0) { changed = 1; return(1); } @@ -183,15 +188,19 @@ term_cbreak(fd) } int -term_copy(src, dst) +term_copy(src, dst, onlcr) int src; int dst; + int onlcr; { struct termios tt; if (tcgetattr(src, &tt) != 0) return(0); - if (tcsetattr(dst, TCSAFLUSH, &tt) != 0) + /* Do not convert line endings from NL to NLCR. */ + if (!onlcr) + CLR(tt.c_oflag, ONLCR); + if (tcsetattr(dst, TCSAFLUSH|TCSASOFT, &tt) != 0) return(0); return(1); } @@ -199,8 +208,9 @@ term_copy(src, dst) #else /* SGTTY */ int -term_raw(fd) +term_raw(fd, onlcr) int fd; + int onlcr; { if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0) return(0); @@ -208,6 +218,9 @@ term_raw(fd) /* Set terminal to raw mode */ CLR(term.c_lflag, ECHO); SET(term.sg_flags, RAW); + /* Retain NL to NLCR conversion if onlcr flag set. */ + if (onlcr) + SET(term.sg_flags, CRMOD); if (ioctl(fd, TIOCSETP, &term) == 0) { changed = 1; return(1); @@ -235,9 +248,10 @@ term_cbreak(fd) } int -term_copy(src, dst) +term_copy(src, dst, onlcr) int src; int dst; + int onlcr; { struct sgttyb b; struct tchars tc; @@ -249,6 +263,9 @@ term_copy(src, dst) ioctl(src, TIOCLGET, &lb)) { return(0); } + /* Do not convert line endings from NL to NLCR. */ + if (!onlcr) + CLR(b.sg_flags, CRMOD); if (ioctl(dst, TIOCSETP, &b) != 0 || ioctl(dst, TIOCSETC, &tc) != 0 || ioctl(dst, TIOCSLTC, &lc) != 0 || ioctl(dst, TIOCLSET, &lb) != 0 || ioctl(dst, TIOCSETD, &l) != 0) { -- 2.49.0