Changeset: 36d925284fee for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/36d925284fee Modified Files: clients/mapiclient/ReadlineTools.c clients/mapiclient/ReadlineTools.h clients/mapiclient/mclient.c clients/mapilib/mapi.c Branch: client_interrupts Log Message:
Do not catch interrupts in the (mapi) library, only in the (mclient) application. diffs (254 lines): diff --git a/clients/mapiclient/ReadlineTools.c b/clients/mapiclient/ReadlineTools.c --- a/clients/mapiclient/ReadlineTools.c +++ b/clients/mapiclient/ReadlineTools.c @@ -423,10 +423,9 @@ bailout: return 1; } -static void -prompt_int_handler(int status) +void +readline_int_handler(void) { - (void)status; printf("\n"); // Move to a new line rl_on_new_line(); // Regenerate the prompt on a newline rl_replace_line("", 0); // Clear the previous text @@ -455,8 +454,6 @@ init_readline(Mapi mid, const char *lang rl_add_funmap_entry("invoke-editor", invoke_editor); rl_bind_keyseq("\\M-e", invoke_editor); - signal(SIGINT, prompt_int_handler); - if (save_history) { int len; if (getenv("HOME") != NULL) { diff --git a/clients/mapiclient/ReadlineTools.h b/clients/mapiclient/ReadlineTools.h --- a/clients/mapiclient/ReadlineTools.h +++ b/clients/mapiclient/ReadlineTools.h @@ -22,6 +22,7 @@ void deinit_readline(void); void save_line(const char *s); rl_completion_func_t *suspend_completion(void); void continue_completion(rl_completion_func_t * func); +void readline_int_handler(void); #endif /* HAVE_LIBREADLINE */ #endif /* READLINETOOLS_H_INCLUDED */ diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c --- a/clients/mapiclient/mclient.c +++ b/clients/mapiclient/mclient.c @@ -1414,17 +1414,21 @@ SQLpagemove(int *len, int fields, int *p SQLseparator(len, fields, '-'); } -static volatile sig_atomic_t stopped; +static volatile sig_atomic_t state; +#define READING 1 +#define WRITING 2 +#define IDLING 0 static void -renderer_sigint_handler(int signum) +sigint_handler(int signum) { - if (signum == SIGINT) { - mnstr_printf(toConsole, "Renderer caught SIGINT\n"); - stopped = true; + (void) signum; +#ifdef HAVE_LIBREADLINE + if (state == READING) { + readline_int_handler(); } - else - mnstr_printf(toConsole, "SQLrenderer: Caught a non-SIGINT signal\n"); +#endif + state = IDLING; } static void @@ -1437,7 +1441,6 @@ SQLrenderer(MapiHdl hdl) int ps = rowsperpage; bool skiprest = false; int64_t rows; /* total number of rows */ - void (*prev_handler)(int); if (ps == 0) ps = pageheight; @@ -1462,12 +1465,7 @@ SQLrenderer(MapiHdl hdl) exit(2); } - stopped = false; - prev_handler = signal(SIGINT, renderer_sigint_handler); - if (prev_handler == SIG_ERR) { - perror("SQLrenderer: Could not install signal handler"); - prev_handler = NULL; - } + state = WRITING; total = 0; lentotal = 0; @@ -1656,8 +1654,7 @@ SQLrenderer(MapiHdl hdl) } } - if (stopped) { - stopped = false; + if (state == IDLING) { mapi_finish(hdl); break; } @@ -1692,8 +1689,6 @@ SQLrenderer(MapiHdl hdl) } mnstr_printf(toConsole, "\n"); - if (prev_handler && signal(SIGINT, prev_handler) == SIG_ERR) - perror("SQLrenderer: Could not restore previous handler"); free(len); free(hdr); free(rest); @@ -2271,7 +2266,9 @@ myread(void *restrict private, void *res if (strcmp(p->prompt, "more>") == 0) func = suspend_completion(); + state = READING; p->buf = readline(p->prompt); + state = IDLING; if (func) continue_completion(func); if (p->buf == NULL) @@ -3221,6 +3218,24 @@ isfile(FILE *fp) return true; } +static void +catch_interrupts(void) +{ +#ifdef HAVE_SIGACTION + struct sigaction sa; + (void) sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = sigint_handler; + if (sigaction(SIGINT, &sa, NULL) == -1) { + perror("Could not install signal handler"); + } +#else + if (signal(SIGINT, sigint_handler) == SIG_ERR) { + perror("Could not install signal handler"); + } +#endif +} + int #ifdef _MSC_VER wmain(int argc, wchar_t **wargv) @@ -3653,6 +3668,8 @@ main(int argc, char **argv) if (!has_fileargs && command == NULL && isatty(fileno(stdin))) { char *lang; + catch_interrupts(); + if (mode == SQL) { lang = "/SQL"; } else { @@ -3752,6 +3769,7 @@ main(int argc, char **argv) if (s == NULL) { if (strcmp(arg, "-") == 0) { + catch_interrupts(); s = stdin_rastream(); } else { s = open_rastream(arg); diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c --- a/clients/mapilib/mapi.c +++ b/clients/mapilib/mapi.c @@ -3365,13 +3365,6 @@ read_file(MapiHdl hdl, uint64_t off, cha } -static void -compute_sigint_handler(int signum) -{ - /* do nothing, but we do need to interrupt the */ - (void) signum; -} - /* Read ahead and cache data read. Depending on the second argument, reading may stop at the first non-header and non-error line, or at a prompt. @@ -3391,11 +3384,6 @@ read_into_cache(MapiHdl hdl, int lookahe char *line; Mapi mid; struct MapiResultSet *result; -#ifdef HAVE_SIGACTION - struct sigaction osa = {0}; -#else - void (*prev_handler)(int) = NULL; -#endif mid = hdl->mid; assert(mid->active == hdl); @@ -3407,35 +3395,9 @@ read_into_cache(MapiHdl hdl, int lookahe if ((result = hdl->active) == NULL) result = hdl->result; /* may also be NULL */ -#ifdef HAVE_SIGACTION - struct sigaction sa; - (void) sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - sa.sa_handler = compute_sigint_handler; - if (sigaction(SIGINT, &sa, &osa) == -1) { - perror("mapi_execute_internal: could not install signal handler"); - (void) sigemptyset(&osa.sa_mask); - osa.sa_flags = 0; - osa.sa_handler = SIG_DFL; - } -#else - prev_handler = signal(SIGINT, compute_sigint_handler); - if (prev_handler == SIG_ERR) { - perror("mapi_execute_internal: could not install signal handler"); - prev_handler = NULL; - } -#endif - for (;;) { line = read_line(mid); if (line == NULL) { -#ifdef HAVE_SIGACTION - if (sigaction(SIGINT, &osa, NULL) == -1) - perror("mapi_execute_internal: Could not restore previous handler"); -#else - if (prev_handler && signal(SIGINT, prev_handler) == SIG_ERR) - perror("mapi_execute_internal: Could not restore previous handler"); -#endif if (mid->from && mnstr_eof(mid->from)) { return mapi_setError(mid, "unexpected end of file", __func__, MERROR); } @@ -3495,13 +3457,6 @@ read_into_cache(MapiHdl hdl, int lookahe } continue; } -#ifdef HAVE_SIGACTION - if (sigaction(SIGINT, &osa, NULL) == -1) - perror("mapi_execute_internal: Could not restore previous handler"); -#else - if (prev_handler && signal(SIGINT, prev_handler) == SIG_ERR) - perror("mapi_execute_internal: Could not restore previous handler"); -#endif return mid->error; case '!': /* start a new result set if we don't have one @@ -3538,13 +3493,6 @@ read_into_cache(MapiHdl hdl, int lookahe (result->querytype == -1 /* unknown (not SQL) */ || result->querytype == Q_TABLE || result->querytype == Q_UPDATE)) { -#ifdef HAVE_SIGACTION - if (sigaction(SIGINT, &osa, NULL) == -1) - perror("mapi_execute_internal: Could not restore previous handler"); -#else - if (prev_handler && signal(SIGINT, prev_handler) == SIG_ERR) - perror("mapi_execute_internal: Could not restore previous handler"); -#endif return mid->error; } break; _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org