Skip to content

Commit

Permalink
Merge pull request #725 from rolandwalker/termios
Browse files Browse the repository at this point in the history
save and restore TTY attributes
  • Loading branch information
jonas authored Sep 30, 2017
2 parents 73e0f77 + e59edc4 commit 35af931
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions include/tig/tig.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <sys/file.h>
#include <time.h>
#include <fcntl.h>
#include <termios.h>

#include <regex.h>

Expand Down
49 changes: 39 additions & 10 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ static WINDOW *display_win[2];
static WINDOW *display_title[2];
static WINDOW *display_sep;

static FILE *opt_tty;
struct display_tty {
FILE *file;
int fd;
struct termios *attr;
};
static struct display_tty opt_tty = { NULL, -1, NULL };

static struct io script_io = { -1 };

Expand Down Expand Up @@ -78,14 +83,16 @@ open_external_viewer(const char *argv[], const char *dir, bool silent, bool conf
clear();
refresh();
endwin(); /* restore original tty modes */
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
ok = io_run_fg(argv, dir);
if (confirm || !ok) {
if (!ok && *notice)
fprintf(stderr, "%s", notice);
fprintf(stderr, "Press Enter to continue");
getc(opt_tty);
fseek(opt_tty, 0, SEEK_END);
getc(opt_tty.file);
}
fseek(opt_tty.file, 0, SEEK_END);
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
set_terminal_modes();
}

Expand Down Expand Up @@ -554,6 +561,12 @@ done_display(void)
endwin();
}
cursed = false;

if (opt_tty.attr) {
tcsetattr(opt_tty.fd, TCSAFLUSH, opt_tty.attr);
free(opt_tty.attr);
opt_tty.attr = NULL;
}
}

static void
Expand All @@ -566,15 +579,32 @@ set_terminal_modes(void)
leaveok(stdscr, false);
}

static void
init_tty(void)
{
/* open */
opt_tty.file = fopen("/dev/tty", "r+");
if (!opt_tty.file)
die("Failed to open tty for input");
opt_tty.fd = fileno(opt_tty.file);

/* attributes */
opt_tty.attr = calloc(1, sizeof(struct termios));
if (!opt_tty.attr)
die("Failed allocation for tty attributes");
tcgetattr(opt_tty.fd, opt_tty.attr);
}

void
init_display(void)
{
bool no_display = !!getenv("TIG_NO_DISPLAY");
const char *term;
int x, y;

init_tty();

die_callback = done_display;
/* XXX: Restore tty modes and let the OS cleanup the rest! */
if (atexit(done_display))
die("Failed to register done_display");

Expand All @@ -583,11 +613,10 @@ init_display(void)
/* Leave stdin and stdout alone when acting as a pager. */
FILE *out_tty;

opt_tty = fopen("/dev/tty", "r+");
out_tty = no_display ? fopen("/dev/null", "w+") : opt_tty;
if (!opt_tty || !out_tty)
die("Failed to open /dev/tty");
cursed = !!newterm(NULL, out_tty, opt_tty);
out_tty = no_display ? fopen("/dev/null", "w+") : opt_tty.file;
if (!out_tty)
die("Failed to open tty for output");
cursed = !!newterm(NULL, out_tty, opt_tty.file);
}

if (!cursed)
Expand Down Expand Up @@ -693,7 +722,7 @@ get_input_char(void)
return key.data.bytes[bytes_pos++];
}

return getc(opt_tty);
return getc(opt_tty.file);
}

int
Expand Down

0 comments on commit 35af931

Please sign in to comment.