From 5eb50e89d12e9e8f89b29145edaf9bdef7e7d597 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 5 Aug 2008 22:01:33 +0200 Subject: Use GLib option parsing instead of getopt --- src/main.c | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 21ef761e..6a25fc54 100644 --- a/src/main.c +++ b/src/main.c @@ -833,21 +833,27 @@ static void sig_debug(int sig) toggle_debug(); } -static void usage(void) -{ - printf("Bluetooth daemon ver %s\n\n", VERSION); - printf("Usage:\n"); - printf("\tbluetoothd [-n] [-d] [-m mtu]\n"); -} +static gboolean option_detach = TRUE; +static gboolean option_debug = FALSE; + +static GOptionEntry options[] = { + { "nodaemon", 'n', G_OPTION_FLAG_REVERSE, + G_OPTION_ARG_NONE, &option_detach, + "Don't fork daemon to background" }, + { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug, + "Enable debug information output" }, + { NULL }, +}; int main(int argc, char *argv[]) { + GOptionContext *context; + GError *err = NULL; struct sockaddr_hci addr; struct hci_filter flt; struct sigaction sa; GIOChannel *ctl_io, *child_io; uint16_t mtu = 0; - int opt, daemonize = 1, debug = 0; GKeyFile *config; /* Default HCId settings */ @@ -865,31 +871,27 @@ int main(int argc, char *argv[]) init_defaults(); - while ((opt = getopt(argc, argv, "ndm:")) != EOF) { - switch (opt) { - case 'n': - daemonize = 0; - break; + context = g_option_context_new(NULL); + g_option_context_add_main_entries(context, options, NULL); - case 'd': - debug = 1; - break; + if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) { + if (err != NULL) { + g_printerr("%s\n", err->message); + g_error_free(err); + } else + g_printerr("An unknown error occurred\n"); + exit(1); + } - case 'm': - mtu = atoi(optarg); - break; + g_option_context_free(context); - default: - usage(); + if (option_detach == TRUE) { + if (daemon(0, 0)) { + perror("Can't start daemon"); exit(1); } } - if (daemonize && daemon(0, 0)) { - error("Can't daemonize: %s (%d)", strerror(errno), errno); - exit(1); - } - umask(0077); start_logging("bluetoothd", "Bluetooth daemon"); @@ -906,7 +908,7 @@ int main(int argc, char *argv[]) sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); - if (debug) { + if (option_debug == TRUE) { info("Enabling debug information"); enable_debug(); } -- cgit