@@ -30,7 +30,12 @@ static WINDOW *display_win[2];
30
30
static WINDOW * display_title [2 ];
31
31
static WINDOW * display_sep ;
32
32
33
- static FILE * opt_tty ;
33
+ struct display_tty {
34
+ FILE * file ;
35
+ int fd ;
36
+ struct termios * attr ;
37
+ };
38
+ static struct display_tty opt_tty = { NULL , -1 , NULL };
34
39
35
40
static struct io script_io = { -1 };
36
41
@@ -78,14 +83,16 @@ open_external_viewer(const char *argv[], const char *dir, bool silent, bool conf
78
83
clear ();
79
84
refresh ();
80
85
endwin (); /* restore original tty modes */
86
+ tcsetattr (opt_tty .fd , TCSAFLUSH , opt_tty .attr );
81
87
ok = io_run_fg (argv , dir );
82
88
if (confirm || !ok ) {
83
89
if (!ok && * notice )
84
90
fprintf (stderr , "%s" , notice );
85
91
fprintf (stderr , "Press Enter to continue" );
86
- getc (opt_tty );
87
- fseek (opt_tty , 0 , SEEK_END );
92
+ getc (opt_tty .file );
88
93
}
94
+ fseek (opt_tty .file , 0 , SEEK_END );
95
+ tcsetattr (opt_tty .fd , TCSAFLUSH , opt_tty .attr );
89
96
set_terminal_modes ();
90
97
}
91
98
@@ -554,6 +561,12 @@ done_display(void)
554
561
endwin ();
555
562
}
556
563
cursed = false;
564
+
565
+ if (opt_tty .attr ) {
566
+ tcsetattr (opt_tty .fd , TCSAFLUSH , opt_tty .attr );
567
+ free (opt_tty .attr );
568
+ opt_tty .attr = NULL ;
569
+ }
557
570
}
558
571
559
572
static void
@@ -566,15 +579,32 @@ set_terminal_modes(void)
566
579
leaveok (stdscr , false);
567
580
}
568
581
582
+ static void
583
+ init_tty (void )
584
+ {
585
+ /* open */
586
+ opt_tty .file = fopen ("/dev/tty" , "r+" );
587
+ if (!opt_tty .file )
588
+ die ("Failed to open tty for input" );
589
+ opt_tty .fd = fileno (opt_tty .file );
590
+
591
+ /* attributes */
592
+ opt_tty .attr = calloc (1 , sizeof (struct termios ));
593
+ if (!opt_tty .attr )
594
+ die ("Failed allocation for tty attributes" );
595
+ tcgetattr (opt_tty .fd , opt_tty .attr );
596
+ }
597
+
569
598
void
570
599
init_display (void )
571
600
{
572
601
bool no_display = !!getenv ("TIG_NO_DISPLAY" );
573
602
const char * term ;
574
603
int x , y ;
575
604
605
+ init_tty ();
606
+
576
607
die_callback = done_display ;
577
- /* XXX: Restore tty modes and let the OS cleanup the rest! */
578
608
if (atexit (done_display ))
579
609
die ("Failed to register done_display" );
580
610
@@ -583,11 +613,10 @@ init_display(void)
583
613
/* Leave stdin and stdout alone when acting as a pager. */
584
614
FILE * out_tty ;
585
615
586
- opt_tty = fopen ("/dev/tty" , "r+" );
587
- out_tty = no_display ? fopen ("/dev/null" , "w+" ) : opt_tty ;
588
- if (!opt_tty || !out_tty )
589
- die ("Failed to open /dev/tty" );
590
- cursed = !!newterm (NULL , out_tty , opt_tty );
616
+ out_tty = no_display ? fopen ("/dev/null" , "w+" ) : opt_tty .file ;
617
+ if (!out_tty )
618
+ die ("Failed to open tty for output" );
619
+ cursed = !!newterm (NULL , out_tty , opt_tty .file );
591
620
}
592
621
593
622
if (!cursed )
@@ -693,7 +722,7 @@ get_input_char(void)
693
722
return key .data .bytes [bytes_pos ++ ];
694
723
}
695
724
696
- return getc (opt_tty );
725
+ return getc (opt_tty . file );
697
726
}
698
727
699
728
int
0 commit comments