From ff7cdf5cf6f80139de016643409bcb4187312b74 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Sun, 14 Jan 2018 23:26:45 -0700 Subject: [PATCH] restore terminal for segfaults and assertions Signed-off-by: Brand Huntsman --- src/nano.c | 17 ++++++++++++++--- src/nano.h | 8 +++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/nano.c b/src/nano.c index a21625cf..71c7a645 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1890,8 +1890,23 @@ void do_output(char *output, size_t output_len, bool allow_cntrls) update_line(openfile->current, openfile->current_x); } +static void segfault_handler(int nothing) +{ + endwin(); + tcsetattr(0, TCSANOW, &oldterm); + #define SEG_FAULT "Segmentation fault\n" + if(write(2, SEG_FAULT, strlen(SEG_FAULT))); + exit(1); +} + int main(int argc, char **argv) { + /* Initialize segfault handler. */ + signal(SIGSEGV, segfault_handler); + + /* Back up the terminal settings so that they can be restored. */ + tcgetattr(0, &oldterm); + int stdin_flags, optchr; #if defined(ENABLED_WRAPORJUSTIFY) && defined(ENABLE_NANORC) bool fill_used = FALSE; @@ -1985,9 +1999,6 @@ int main(int argc, char **argv) console = (getenv("DISPLAY") == NULL); #endif - /* Back up the terminal settings so that they can be restored. */ - tcgetattr(0, &oldterm); - /* Get the state of standard input and ensure it uses blocking mode. */ stdin_flags = fcntl(0, F_GETFL, 0); if (stdin_flags != -1) diff --git a/src/nano.h b/src/nano.h index 55abf1e8..860175f0 100644 --- a/src/nano.h +++ b/src/nano.h @@ -120,7 +120,14 @@ #include #include #include -#include +#include + +#define assert(expr) if(!(expr)){ \ + endwin(); \ + tcsetattr(0, TCSANOW, &oldterm); \ + fprintf(stderr, "Assertion (%s) failed: %s %s() %d\n", #expr, __FILE__, __func__, __LINE__); \ + exit(1); \ +} /* If we aren't using an ncurses with mouse support, exclude any * mouse routines, as they are useless then. */ -- 2.13.6