improve tcgetattr and setup() - sob - simple output bar
 (HTM) git clone git://git.codemadness.org/sob
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 7a89c043b1e2c774e6522b3b1b3a4114354ee0fd
 (DIR) parent 7eae1acf0a7d4644114e30f6f3bfc2143954518c
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Sat, 25 Oct 2014 11:12:50 +0000
       
       improve tcgetattr and setup()
       
       - don't restore attributes if tcgetattr failed.
       - tcgetattr indicates if it's isatty().
       
       Diffstat:
         M sob.c                               |      57 +++++++++++++++++++------------
       
       1 file changed, 36 insertions(+), 21 deletions(-)
       ---
 (DIR) diff --git a/sob.c b/sob.c
       @@ -756,14 +756,29 @@ sighandler(int signum)
        static void
        setup(void)
        {
       -        tcgetattr(STDIN_FILENO, &ttystate);
       -        ttysave = ttystate;
       -        /* turn off canonical mode and echo */
       -        ttystate.c_lflag &= ~(ICANON | ECHO);
       -        ttystate.c_cc[VMIN] = 1;
       -
       -        /* set the terminal attributes */
       -        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +        if(tcgetattr(STDIN_FILENO, &ttystate) == 0) {
       +                termattrset = 1;
       +                ttysave = ttystate;
       +                /* turn off canonical mode and echo */
       +                ttystate.c_lflag &= ~(ICANON | ECHO);
       +                ttystate.c_cc[VMIN] = 1;
       +                /* set the terminal attributes */
       +                tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +        } else {
       +                /* not a tty */
       +                initialinput();
       +                /* setup tty again because we (re)open "/dev/tty" */
       +                termattrset = 0;
       +                if(tcgetattr(STDIN_FILENO, &ttystate) == 0) {
       +                        termattrset = 1;
       +                        ttysave = ttystate;
       +                        /* turn off canonical mode and echo */
       +                        ttystate.c_lflag &= ~(ICANON | ECHO);
       +                        ttystate.c_cc[VMIN] = 1;
       +                        /* set the terminal attributes */
       +                        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +                }
       +        }
                /* get terminal window size */
                gettermsize();
        }
       @@ -900,10 +915,12 @@ initialinput(void)
                        buf[r] = '\0';
                        handleinput(buf, r);
                }
       -        /* restore terminal attributes. */
       -        ttystate.c_lflag = ttysave.c_lflag;
       -        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       -        /* close and reattach to stdin */
       +        /* restore terminal attributes */
       +        if(termattrset) {
       +                ttystate.c_lflag = ttysave.c_lflag;
       +                tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +        }
       +        /* close and re-attach to stdin */
                close(STDIN_FILENO);
                if((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) == -1) {
                        fprintf(stderr, "open: /dev/tty: %s\n", strerror(errno));
       @@ -931,7 +948,6 @@ main(int argc, char **argv)
                lineoutfp = stdout;
                outfp = stderr;
                setlocale(LC_ALL, "");
       -        setup();
        
                /* signal handling */
                memset(&sa, 0, sizeof(sa));
       @@ -943,18 +959,17 @@ main(int argc, char **argv)
                sa.sa_handler = SIG_IGN;
                sigaction(SIGCHLD, &sa, NULL);
        
       -        if(!isatty(STDIN_FILENO)) {
       -                initialinput();
       -                /* setup tty again because we (re)open "/dev/tty" */
       -                setup();
       -        }
       +        setup();
       +
                /* clear screen */
                clear();
                run();
        
       -        /* cleanup: restore terminal attributes. */
       -        ttystate.c_lflag = ttysave.c_lflag;
       -        tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +        /* cleanup: restore terminal attributes */
       +        if(termattrset) {
       +                ttystate.c_lflag = ttysave.c_lflag;
       +                tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
       +        }
        
                return EXIT_SUCCESS;
        }