Skip to content

Commit

Permalink
Make sure we reset terminal config also on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisklein committed Oct 10, 2018
1 parent a78d35d commit 9f32545
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions fairmq/plugins/Control.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ auto ControlPluginProgramOptions() -> Plugin::ProgOptions
return pluginOptions;
}

struct terminal_config
{
terminal_config()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag &= ~ICANON; // disable canonical input
t.c_lflag &= ~ECHO; // do not echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}

~terminal_config()
{
termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag |= ICANON; // re-enable canonical input
t.c_lflag |= ECHO; // echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
}
};

auto Control::InteractiveMode() -> void
try
{
Expand All @@ -122,12 +143,9 @@ try
pollfd cinfd[1];
cinfd[0].fd = fileno(stdin);
cinfd[0].events = POLLIN;
cinfd[0].revents = 0;

struct termios t;
tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag &= ~ICANON; // disable canonical input
t.c_lflag &= ~ECHO; // do not echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings
terminal_config tconfig;

PrintInteractiveHelp();

Expand Down Expand Up @@ -220,11 +238,6 @@ try
}
}

tcgetattr(STDIN_FILENO, &t); // get the current terminal I/O structure
t.c_lflag |= ICANON; // re-enable canonical input
t.c_lflag |= ECHO; // echo input chars
tcsetattr(STDIN_FILENO, TCSANOW, &t); // apply the new settings

RunShutdownSequence();
}
catch (PluginServices::DeviceControlError& e)
Expand Down

0 comments on commit 9f32545

Please sign in to comment.