From 3a3c807addf81adcc183aa107cab4ea594da1499 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 23 May 2019 11:24:16 +0200 Subject: [PATCH] dispatcher: parse command line arguments in a seprate function Split command line parsing out of the main() function. For one, it's an self-contained step, so we can make main() simpler. Also, we don't need the GOptionEntry on the stack of the main() function for the remainder of the program. --- dispatcher/nm-dispatcher.c | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/dispatcher/nm-dispatcher.c b/dispatcher/nm-dispatcher.c index a812a870b9..8f1d45a1ba 100644 --- a/dispatcher/nm-dispatcher.c +++ b/dispatcher/nm-dispatcher.c @@ -960,34 +960,45 @@ signal_handler (gpointer user_data) return G_SOURCE_CONTINUE; } +static gboolean +parse_command_line (int *p_argc, + char ***p_argv, + GError **error) +{ + GOptionContext *opt_ctx; + GOptionEntry entries[] = { + { "debug", 0, 0, G_OPTION_ARG_NONE, &gl.debug, "Output to console rather than syslog", NULL }, + { "persist", 0, 0, G_OPTION_ARG_NONE, &gl.persist, "Don't quit after a short timeout", NULL }, + { NULL } + }; + gboolean success; + + opt_ctx = g_option_context_new (NULL); + g_option_context_set_summary (opt_ctx, "Executes scripts upon actions by NetworkManager."); + g_option_context_add_main_entries (opt_ctx, entries, NULL); + + success = g_option_context_parse (opt_ctx, p_argc, p_argv, error); + + g_option_context_free (opt_ctx); + + return success; +} + int main (int argc, char **argv) { - GOptionContext *opt_ctx; gs_free_error GError *error = NULL; GDBusConnection *bus; Handler *handler = NULL; guint signal_id_term = 0; guint signal_id_int = 0; - GOptionEntry entries[] = { - { "debug", 0, 0, G_OPTION_ARG_NONE, &gl.debug, "Output to console rather than syslog", NULL }, - { "persist", 0, 0, G_OPTION_ARG_NONE, &gl.persist, "Don't quit after a short timeout", NULL }, - { NULL } - }; - - opt_ctx = g_option_context_new (NULL); - g_option_context_set_summary (opt_ctx, "Executes scripts upon actions by NetworkManager."); - g_option_context_add_main_entries (opt_ctx, entries, NULL); - - if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) { + if (!parse_command_line (&argc, &argv, &error)) { _LOG_X_W ("Error parsing command line arguments: %s", error->message); gl.exit_with_failure = TRUE; goto done; } - g_option_context_free (opt_ctx); - signal_id_term = g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM)); signal_id_int = g_unix_signal_add (SIGINT, signal_handler, GINT_TO_POINTER (SIGINT));