Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save and restore TTY attributes #725

Merged
merged 2 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will open /dev/tty for tests. I think it would be better to skip this if no_display is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally so — from the point of view of the OS, a TTY is much more than a display.

The TTY is also the medium for the line discipline, in rough terms the interactive input.

A TTY is also inseparable from process-group leadership via tcsetpgrp(), which among other things determines the disposition of signals, which is relevant under the test harness.

Also, regardless of whether a display is visible, tig is still linked with ncurses, and ncurses maintains a thoroughly TTY-oriented understanding of the world in which it runs. The goal I was driving for here is for ncurses to keep an input-only TTY under no_display. Tig still does not display anything, but the input "side" of ncurses remains connected to a proper ioctlable TTY.

The separate matter of process-group leadership becomes relevant when there are long-lived child processes as proposed in #301/#734. The simplest and most robust way to handle child cleanup is to

In the most ordinary case, the interactive shell makes tig the process-group leader transparently, and we don't have to think about it. However, that magic does not happen in non-interactive cases, such as when driven by tig-pick or the test harness (where no_display happens to be set). So it is a good idea, when you know you may be invoked from a script, to grab a TTY as a step toward becoming process-group leader.

Congrats on the release!


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