From 12f284dee3f6d9130eb0cf1bcb287c9c69d31e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 23 Jun 2014 13:59:46 +0200 Subject: [PATCH] cli: make Ctrl-C exit nmcli when line is empty *and* we are not in the editor --- cli/src/common.c | 24 ++++++++++++++++++++---- cli/src/connections.c | 3 +++ cli/src/nmcli.c | 2 +- cli/src/nmcli.h | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cli/src/common.c b/cli/src/common.c index 63399dd2c8..1ee5c09976 100644 --- a/cli/src/common.c +++ b/cli/src/common.c @@ -1124,6 +1124,9 @@ nmc_set_in_readline (gboolean in_readline) pthread_mutex_unlock (&readline_mutex); } +/* Global variable defined in nmcli.c */ +extern NmCli nm_cli; + /** * nmc_readline: * @prompt_fmt: prompt to print (telling user what to enter). It is standard @@ -1131,7 +1134,8 @@ nmc_set_in_readline (gboolean in_readline) * @...: a list of arguments according to the @prompt_fmt format string * * Wrapper around libreadline's readline() function. - * If user pressed Ctrl-C, readline() is called again. + * If user pressed Ctrl-C, readline() is called again (if not in editor and + * line is empty, nmcli will quit). * If user pressed Ctrl-D on empty line, nmcli will quit. * * Returns: the user provided string. In case the user entered empty string, @@ -1168,11 +1172,23 @@ readline_mark: for (;;) sleep (3); } - /* In case of Ctrl-C we call readline again to get new prompt (repeat) */ + /* Ctrl-C */ if (nmc_seen_sigint ()) { nmc_clear_sigint (); - g_free (str); - goto readline_mark; + if (nm_cli.in_editor || *str) { + /* In editor, or the line is not empty */ + /* Call readline again to get new prompt (repeat) */ + g_free (str); + goto readline_mark; + } else { + /* Not in editor and line is empty */ + /* Send SIGQUIT to itself */ + nmc_set_sigquit_internal (); + kill (getpid (), SIGQUIT); + /* Sleep in this thread so that we don't do anything else until exit */ + for (;;) + sleep (3); + } } g_free (prompt); diff --git a/cli/src/connections.c b/cli/src/connections.c index d17f7f1264..c93e3351ab 100644 --- a/cli/src/connections.c +++ b/cli/src/connections.c @@ -7982,6 +7982,9 @@ do_connection_edit (NmCli *nmc, int argc, char **argv) editor_init_new_connection (nmc, connection); } + /* nmcli runs the editor */ + nmc->in_editor = TRUE; + printf ("\n"); printf (_("===| nmcli interactive connection editor |===")); printf ("\n\n"); diff --git a/cli/src/nmcli.c b/cli/src/nmcli.c index b4d81ff693..ffd2cdbbc5 100644 --- a/cli/src/nmcli.c +++ b/cli/src/nmcli.c @@ -307,7 +307,6 @@ event_hook_for_readline (void) /* Make readline() exit on SIGINT */ if (nmc_seen_sigint ()) { rl_echo_signal_char (SIGINT); - rl_delete_text (0, rl_end); rl_stuff_char ('\n'); } return 0; @@ -433,6 +432,7 @@ nmc_init (NmCli *nmc) memset (&nmc->print_fields, '\0', sizeof (NmcPrintFields)); nmc->nocheck_ver = FALSE; nmc->ask = FALSE; + nmc->in_editor = FALSE; nmc->editor_status_line = FALSE; nmc->editor_save_confirmation = TRUE; nmc->editor_prompt_color = NMC_TERM_COLOR_NORMAL; diff --git a/cli/src/nmcli.h b/cli/src/nmcli.h index dd5b7d194d..fd211b107c 100644 --- a/cli/src/nmcli.h +++ b/cli/src/nmcli.h @@ -128,6 +128,7 @@ typedef struct _NmCli { NmcPrintFields print_fields; /* Structure with field indices to print */ gboolean nocheck_ver; /* Don't check nmcli and NM versions: option '--nocheck' */ gboolean ask; /* Ask for missing parameters: option '--ask' */ + gboolean in_editor; /* Whether running the editor - nmcli con edit' */ gboolean editor_status_line; /* Whether to display status line in connection editor */ gboolean editor_save_confirmation; /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */ NmcTermColor editor_prompt_color; /* Color of prompt in connection editor */