From 3407726758e5de86c720e6e0e35835ccfbceb290 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 11 Mar 2013 12:06:38 -0400 Subject: [PATCH] config: move config-related command-line options into nm-config.c Rather than having main.c parse them and then hand them all to nm_config_new(), just let nm-config provide its own GOptionEntry array to merge in with main's. --- po/POTFILES.in | 1 + src/config/nm-config.c | 53 +++++++++++++++++++++++++++++----- src/config/nm-config.h | 12 +++----- src/config/tests/test-config.c | 53 ++++++++++++++++++++++++++-------- src/main.c | 30 ++----------------- 5 files changed, 93 insertions(+), 56 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index d1a6153cfa..43b8fc186b 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -40,6 +40,7 @@ src/dhcp-manager/nm-dhcp-dhclient-utils.c src/dhcp-manager/nm-dhcp-manager.c src/dns-manager/nm-dns-manager.c src/logging/nm-logging.c +src/config/nm-config.c src/modem-manager/nm-modem-cdma.c src/modem-manager/nm-modem-gsm.c src/modem-manager/nm-modem-broadband.c diff --git a/src/config/nm-config.c b/src/config/nm-config.c index 78e8b3e061..8108786911 100644 --- a/src/config/nm-config.c +++ b/src/config/nm-config.c @@ -25,6 +25,8 @@ #include "nm-config.h" +#include + #define NM_DEFAULT_SYSTEM_CONF_FILE NMCONFDIR "/NetworkManager.conf" #define NM_OLD_SYSTEM_CONF_FILE NMCONFDIR "/nm-system-settings.conf" @@ -123,6 +125,40 @@ nm_config_get_connectivity_response (NMConfig *config) return NM_CONFIG_GET_PRIVATE (config)->connectivity_response; } +/************************************************************************/ + +static char *cli_config_path; +static char *cli_plugins; +static char *cli_log_level; +static char *cli_log_domains; +static char *cli_connectivity_uri; +static int cli_connectivity_interval = -1; +static char *cli_connectivity_response; + +static GOptionEntry config_options[] = { + { "config", 0, 0, G_OPTION_ARG_FILENAME, &cli_config_path, N_("Config file location"), N_("/path/to/config.file") }, + { "plugins", 0, 0, G_OPTION_ARG_STRING, &cli_plugins, N_("List of plugins separated by ','"), N_("plugin1,plugin2") }, + /* Translators: Do not translate the values in the square brackets */ + { "log-level", 0, 0, G_OPTION_ARG_STRING, &cli_log_level, N_("Log level: one of [ERR, WARN, INFO, DEBUG]"), "INFO" }, + { "log-domains", 0, 0, G_OPTION_ARG_STRING, &cli_log_domains, + /* Translators: Do not translate the values in the square brackets */ + N_("Log domains separated by ',': any combination of\n" + " [NONE,HW,RFKILL,ETHER,WIFI,BT,MB,DHCP4,DHCP6,PPP,\n" + " WIFI_SCAN,IP4,IP6,AUTOIP4,DNS,VPN,SHARING,SUPPLICANT,\n" + " AGENTS,SETTINGS,SUSPEND,CORE,DEVICE,OLPC,WIMAX,\n" + " INFINIBAND,FIREWALL,ADSL]"), + "HW,RFKILL,WIFI" }, + { "connectivity-uri", 0, 0, G_OPTION_ARG_STRING, &cli_connectivity_uri, N_("An http(s) address for checking internet connectivity"), "http://example.com" }, + { "connectivity-interval", 0, 0, G_OPTION_ARG_INT, &cli_connectivity_interval, N_("The interval between connectivity checks (in seconds)"), "60" }, + { "connectivity-response", 0, 0, G_OPTION_ARG_STRING, &cli_connectivity_response, N_("The expected start of the response"), N_("Bingo!") }, + {NULL} +}; + +GOptionEntry * +nm_config_get_options (void) +{ + return config_options; +} /************************************************************************/ @@ -185,14 +221,7 @@ nm_config_get (void) /* call this function only once! */ NMConfig * -nm_config_new (const char *cli_config_path, - const char *cli_plugins, - const char *cli_log_level, - const char *cli_log_domains, - const char *cli_connectivity_uri, - const gint cli_connectivity_interval, - const char *cli_connectivity_response, - GError **error) +nm_config_new (GError **error) { GError *local = NULL; NMConfigPrivate *priv = NULL; @@ -298,6 +327,14 @@ finalize (GObject *gobject) g_free (priv->connectivity_response); singleton = NULL; + + g_clear_pointer (&cli_config_path, g_free); + g_clear_pointer (&cli_plugins, g_free); + g_clear_pointer (&cli_log_level, g_free); + g_clear_pointer (&cli_log_domains, g_free); + g_clear_pointer (&cli_connectivity_uri, g_free); + g_clear_pointer (&cli_connectivity_response, g_free); + G_OBJECT_CLASS (nm_config_parent_class)->finalize (gobject); } diff --git a/src/config/nm-config.h b/src/config/nm-config.h index ef351c1b2e..dbdc2f0ae4 100644 --- a/src/config/nm-config.h +++ b/src/config/nm-config.h @@ -45,14 +45,6 @@ typedef struct { GType nm_config_get_type (void); NMConfig *nm_config_get (void); -NMConfig *nm_config_new (const char *cli_config_path, - const char *cli_plugins, - const char *cli_log_level, - const char *cli_log_domains, - const char *cli_connectivity_check_uri, - const gint connectivity_check_interval, - const char *cli_connectivity_check_response, - GError **error); const char *nm_config_get_path (NMConfig *config); const char **nm_config_get_plugins (NMConfig *config); @@ -64,6 +56,10 @@ const char *nm_config_get_connectivity_uri (NMConfig *config); const guint nm_config_get_connectivity_interval (NMConfig *config); const char *nm_config_get_connectivity_response (NMConfig *config); +/* for main.c only */ +GOptionEntry *nm_config_get_options (void); +NMConfig *nm_config_new (GError **error); + G_END_DECLS #endif /* NM_CONFIG_H */ diff --git a/src/config/tests/test-config.c b/src/config/tests/test-config.c index 9b70828385..e2da79ffd6 100644 --- a/src/config/tests/test-config.c +++ b/src/config/tests/test-config.c @@ -22,6 +22,36 @@ #include +static void +setup_config (const char *config_file, ...) +{ + va_list ap; + GPtrArray *args; + char **argv, *arg; + int argc; + GOptionContext *context; + + args = g_ptr_array_new (); + g_ptr_array_add (args, "test-config"); + g_ptr_array_add (args, "--config"); + g_ptr_array_add (args, (char *)config_file); + + va_start (ap, config_file); + while ((arg = va_arg (ap, char *))) + g_ptr_array_add (args, arg); + va_end (ap); + + argv = (char **)args->pdata; + argc = args->len; + + context = g_option_context_new (NULL); + g_option_context_add_main_entries (context, nm_config_get_options (), NULL); + g_option_context_parse (context, &argc, &argv, NULL); + g_option_context_free (context); + + g_ptr_array_free (args, TRUE); +} + static void test_config_simple (void) { @@ -29,9 +59,8 @@ test_config_simple (void) GError *error = NULL; const char **plugins; - config = nm_config_new (SRCDIR "/NetworkManager.conf", - NULL, NULL, NULL, NULL, -1, NULL, - &error); + setup_config (SRCDIR "/NetworkManager.conf", NULL); + config = nm_config_new (&error); g_assert_no_error (error); g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); @@ -54,9 +83,8 @@ test_config_non_existent (void) NMConfig *config; GError *error = NULL; - config = nm_config_new (SRCDIR "/no-such-file", - NULL, NULL, NULL, NULL, -1, NULL, - &error); + setup_config (SRCDIR "/no-such-file", NULL); + config = nm_config_new (&error); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND); } @@ -66,9 +94,8 @@ test_config_parse_error (void) NMConfig *config; GError *error = NULL; - config = nm_config_new (SRCDIR "/bad.conf", - NULL, NULL, NULL, NULL, -1, NULL, - &error); + setup_config (SRCDIR "/bad.conf", NULL); + config = nm_config_new (&error); g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE); } @@ -79,9 +106,11 @@ test_config_override (void) GError *error = NULL; const char **plugins; - config = nm_config_new (SRCDIR "/NetworkManager.conf", - "alpha,beta,gamma,delta", NULL, NULL, NULL, 12, NULL, - &error); + setup_config (SRCDIR "/NetworkManager.conf", + "--plugins", "alpha,beta,gamma,delta", + "--connectivity-interval", "12", + NULL); + config = nm_config_new (&error); g_assert_no_error (error); g_assert_cmpstr (nm_config_get_path (config), ==, SRCDIR "/NetworkManager.conf"); diff --git a/src/main.c b/src/main.c index d2b77ae803..dff1bdb261 100644 --- a/src/main.c +++ b/src/main.c @@ -305,11 +305,6 @@ main (int argc, char *argv[]) gboolean become_daemon = FALSE; gboolean g_fatal_warnings = FALSE; char *pidfile = NULL, *state_file = NULL; - char *config_path = NULL, *plugins = NULL; - char *log_level = NULL, *log_domains = NULL; - char *connectivity_uri = NULL; - gint connectivity_interval = -1; - char *connectivity_response = NULL; gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; gboolean success, show_version = FALSE; NMPolicy *policy = NULL; @@ -330,21 +325,6 @@ main (int argc, char *argv[]) { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, N_("Make all warnings fatal"), NULL }, { "pid-file", 0, 0, G_OPTION_ARG_FILENAME, &pidfile, N_("Specify the location of a PID file"), N_("filename") }, { "state-file", 0, 0, G_OPTION_ARG_FILENAME, &state_file, N_("State file location"), N_("/path/to/state.file") }, - { "config", 0, 0, G_OPTION_ARG_FILENAME, &config_path, N_("Config file location"), N_("/path/to/config.file") }, - { "plugins", 0, 0, G_OPTION_ARG_STRING, &plugins, N_("List of plugins separated by ','"), N_("plugin1,plugin2") }, - /* Translators: Do not translate the values in the square brackets */ - { "log-level", 0, 0, G_OPTION_ARG_STRING, &log_level, N_("Log level: one of [ERR, WARN, INFO, DEBUG]"), "INFO" }, - { "log-domains", 0, 0, G_OPTION_ARG_STRING, &log_domains, - /* Translators: Do not translate the values in the square brackets */ - N_("Log domains separated by ',': any combination of\n" - " [NONE,HW,RFKILL,ETHER,WIFI,BT,MB,DHCP4,DHCP6,PPP,\n" - " WIFI_SCAN,IP4,IP6,AUTOIP4,DNS,VPN,SHARING,SUPPLICANT,\n" - " AGENTS,SETTINGS,SUSPEND,CORE,DEVICE,OLPC,WIMAX,\n" - " INFINIBAND,FIREWALL,ADSL]"), - "HW,RFKILL,WIFI" }, - { "connectivity-uri", 0, 0, G_OPTION_ARG_STRING, &connectivity_uri, _("An http(s) address for checking internet connectivity"), "http://example.com" }, - { "connectivity-interval", 0, 0, G_OPTION_ARG_INT, &connectivity_interval, _("The interval between connectivity checks (in seconds)"), "60" }, - { "connectivity-response", 0, 0, G_OPTION_ARG_STRING, &connectivity_response, _("The expected start of the response"), N_("Bingo!") }, {NULL} }; @@ -366,6 +346,7 @@ main (int argc, char *argv[]) g_option_context_set_ignore_unknown_options (opt_ctx, FALSE); g_option_context_set_help_enabled (opt_ctx, TRUE); g_option_context_add_main_entries (opt_ctx, options, NULL); + g_option_context_add_main_entries (opt_ctx, nm_config_get_options (), NULL); g_option_context_set_summary (opt_ctx, _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with.")); @@ -414,8 +395,7 @@ main (int argc, char *argv[]) exit (1); /* Read the config file and CLI overrides */ - config = nm_config_new (config_path, plugins, log_level, log_domains, - connectivity_uri, connectivity_interval, connectivity_response, &error); + config = nm_config_new (&error); if (config == NULL) { fprintf (stderr, _("Failed to read configuration: (%d) %s\n"), error ? error->code : -1, @@ -640,12 +620,6 @@ done: /* Free options */ g_free (pidfile); g_free (state_file); - g_free (config_path); - g_free (plugins); - g_free (log_level); - g_free (log_domains); - g_free (connectivity_uri); - g_free (connectivity_response); nm_log_info (LOGD_CORE, "exiting (%s)", success ? "success" : "error"); exit (success ? 0 : 1);