Skip to content

Commit

Permalink
save stdin terminal settings on import hoc and restore on h.quit()
Browse files Browse the repository at this point in the history
  • Loading branch information
nrnhines committed Nov 29, 2024
1 parent 7dd6e6c commit dd7514a
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/nrnpython/inithoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,20 @@ static int have_opt(const char* arg) {
#include <termios.h>
#include <unistd.h>

static void set_terminal_sane() {
struct termios settings;
static struct termios original_termios;

if (tcgetattr(STDIN_FILENO, &settings) != 0) {
std::cerr << "Error getting terminal attributes\r\n";
return;
static void save_original_terminal_settings() {
if (tcgetattr(STDIN_FILENO, &original_termios) == -1) {
std::cerr << "Error getting original terminal attributes\n";
}
}

// Set the specific attributes back to their sane values
settings.c_iflag |= (BRKINT | IMAXBEL);
settings.c_iflag &= ~(IGNBRK | INLCR | IXOFF | IUTF8);

settings.c_oflag |= (OPOST | ONLCR); // ONLCR gives correct newlines
settings.c_oflag &= ~(ONLRET | OFILL | ONOCR); // Clear flags not needed

settings.c_cflag |= (CS8);

settings.c_lflag |= (ISIG | ICANON | IEXTEN | ECHO);

if (tcsetattr(STDIN_FILENO, TCSANOW, &settings) != 0) {
std::cerr << "Error setting terminal attributes\r\n";
static void restore_original_terminal_settings() {
if (tcsetattr(STDIN_FILENO, TCSANOW, &original_termios) == -1) {
std::cerr << "Error restoring terminal attributes\n";

Check warning on line 232 in src/nrnpython/inithoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/inithoc.cpp#L230-L232

Added lines #L230 - L232 were not covered by tests
}
}

Check warning on line 234 in src/nrnpython/inithoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/inithoc.cpp#L234

Added line #L234 was not covered by tests
#endif // __linux__
#endif // __linux__

void nrnpython_finalize() {
#if NRN_ENABLE_THREADS
Expand All @@ -253,7 +243,7 @@ void nrnpython_finalize() {
Py_Finalize();
}
#if __linux__
set_terminal_sane();
restore_original_terminal_settings();

Check warning on line 246 in src/nrnpython/inithoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/inithoc.cpp#L246

Added line #L246 was not covered by tests
#endif
}

Expand All @@ -264,6 +254,10 @@ extern "C" NRN_EXPORT PyObject* PyInit_hoc() {
main_thread_ = std::this_thread::get_id();
#endif

#if __linux__
save_original_terminal_settings();
#endif // __linux__

if (nrn_global_argv) { // ivocmain was already called so already loaded
return nrnpy_hoc();
}
Expand Down

0 comments on commit dd7514a

Please sign in to comment.