]> granicus.if.org Git - sudo/commitdiff
Retain NL to NLCR conversion on the real tty and skip it on the pty
authorTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 30 Sep 2009 02:12:35 +0000 (02:12 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 30 Sep 2009 02:12:35 +0000 (02:12 +0000)
we allocate.  That way, if stdout is not a pty there are no extra carriage
returns.

script.c
sudo.h
term.c

index 30fc3adc94652c01c1e7942b9305d4992b3dc0c4..6440d6dabda2d3a63074263173d51e65ea53467b 100644 (file)
--- 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 0843abbd2d49c5b30ab7d9ea2eaa4be0a7dc3855..0db98fa47a470af2d6ec9be225160e7c3a6a841f 100644 (file)
--- 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 92f381291eaeee01fee4e090aaa64abd9ff15d20..9811673c1d4732cb6d0ed22ba9039f9872eb1783 100644 (file)
--- 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) {