diff options
author | Thomas Haller <thaller@redhat.com> | 2019-05-23 11:24:16 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-05-27 12:07:52 +0200 |
commit | 3a3c807addf81adcc183aa107cab4ea594da1499 (patch) | |
tree | deac837ab7d65cf2126a1e98099dbe98bfa77cec /dispatcher | |
parent | 866189a001987a2f475685bbb324fba53e1ffd2e (diff) | |
download | NetworkManager-3a3c807addf81adcc183aa107cab4ea594da1499.tar.gz |
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.
Diffstat (limited to 'dispatcher')
-rw-r--r-- | dispatcher/nm-dispatcher.c | 33 |
1 files changed, 22 insertions, 11 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; } -int -main (int argc, char **argv) +static gboolean +parse_command_line (int *p_argc, + char ***p_argv, + GError **error) { 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 } }; + 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); - if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) { + 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) +{ + gs_free_error GError *error = NULL; + GDBusConnection *bus; + Handler *handler = NULL; + guint signal_id_term = 0; + guint signal_id_int = 0; + + 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)); |