From 4202ba0ea10cc6d994bc28f55f37d0ef93a08e1e Mon Sep 17 00:00:00 2001 From: Nicolas Trangez Date: Sun, 28 May 2006 02:55:44 +0000 Subject: [PATCH] 2006-05-26 Nicolas Trangez * src/NetworkManager.c: use GOptions instead of getopt * configure.in: bump glib required version to >= 2.6 for GOption support git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@1771 4912f4e0-d625-0410-9fb7-b9a5a253dbdc --- ChangeLog | 6 +++++ configure.in | 2 +- src/NetworkManager.c | 61 ++++++++++++++------------------------------ 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3a6832f62b..0a64ee1354 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-05-26 Nicolas Trangez + + * src/NetworkManager.c: use GOptions instead of getopt + * configure.in: bump glib required version to >= 2.6 for GOption + support + 2006-05-25 Robert Love * src/nm-device.h: Introduce nm_ioctl_info(), which defines to diff --git a/configure.in b/configure.in index 13b1b616c4..a3da0a4f99 100644 --- a/configure.in +++ b/configure.in @@ -139,7 +139,7 @@ PKG_CHECK_MODULES(GTHREAD, gthread-2.0) AC_SUBST(GTHREAD_CFLAGS) AC_SUBST(GTHREAD_LIBS) -PKG_CHECK_MODULES(GLIB, glib-2.0) +PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.6) AC_SUBST(GLIB_CFLAGS) AC_SUBST(GLIB_LIBS) diff --git a/src/NetworkManager.c b/src/NetworkManager.c index d459295d29..26caa73f36 100644 --- a/src/NetworkManager.c +++ b/src/NetworkManager.c @@ -655,13 +655,7 @@ write_pidfile (const char *pidfile) */ static void nm_print_usage (void) { - fprintf (stderr, "\n" "usage : NetworkManager [--no-daemon] [--pid-file=] [--help]\n"); fprintf (stderr, - "\n" - " --no-daemon Don't become a daemon\n" - " --pid-file= Specify the location of a PID file\n" - " --enable-test-devices Allow dummy devices to be created via DBUS methods [DEBUG]\n" - " --help Show this information and exit\n" "\n" "NetworkManager monitors all network connections and automatically\n" "chooses the best connection to use. It also allows the user to\n" @@ -677,8 +671,9 @@ static void nm_print_usage (void) */ int main( int argc, char *argv[] ) { - gboolean become_daemon = TRUE; + gboolean become_daemon = FALSE; gboolean enable_test_devices = FALSE; + gboolean show_usage = FALSE; char * owner; char * pidfile = NULL; char * user_pidfile = NULL; @@ -694,47 +689,29 @@ int main( int argc, char *argv[] ) textdomain (GETTEXT_PACKAGE); /* Parse options */ - while (1) { - int c; - int option_index = 0; - const char *opt; - - static struct option options[] = { - {"no-daemon", 0, NULL, 0}, - {"enable-test-devices", 0, NULL, 0}, - {"pid-file", 1, NULL, 0}, - {"help", 0, NULL, 0}, - {NULL, 0, NULL, 0} + GOptionContext *opt_ctx = NULL; + GOptionEntry options[] = { + {"no-daemon", 0, 0, G_OPTION_ARG_NONE, &become_daemon, "Don't become a daemon", NULL}, + {"pid-file", 0, 0, G_OPTION_ARG_STRING, &user_pidfile, "Specify the location of a PID file", NULL}, + {"enable-test-devices", 0, 0, G_OPTION_ARG_NONE, &enable_test_devices, "Allow dummy devices to be created via DBUS methods [DEBUG]", NULL}, + {"info", 0, 0, G_OPTION_ARG_NONE, &show_usage, "Show application information", NULL}, + {NULL} }; + opt_ctx = g_option_context_new(""); + g_option_context_add_main_entries(opt_ctx, options, NULL); + g_option_context_parse(opt_ctx, &argc, &argv, NULL); + g_option_context_free(opt_ctx); + } - c = getopt_long (argc, argv, "", options, &option_index); - if (c == -1) - break; - - switch (c) + /* Tricky: become_daemon is FALSE by default, so unless it's TRUE because of a CLI + * option, it'll become TRUE after this */ + become_daemon = !become_daemon; + if (show_usage == TRUE) { - case 0: - opt = options[option_index].name; - if (strcmp (opt, "help") == 0) - { - nm_print_usage (); + nm_print_usage(); exit (EXIT_SUCCESS); } - else if (strcmp (opt, "no-daemon") == 0) - become_daemon = FALSE; - else if (strcmp (opt, "enable-test-devices") == 0) - enable_test_devices = TRUE; - else if (strcmp (opt, "pid-file") == 0) - user_pidfile = g_strdup (optarg); - break; - - default: - nm_print_usage (); - exit (EXIT_FAILURE); - break; - } - } if (become_daemon) {