summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2008-08-05 22:01:33 +0200
committerMarcel Holtmann <marcel@holtmann.org>2008-08-05 22:01:33 +0200
commit5eb50e89d12e9e8f89b29145edaf9bdef7e7d597 (patch)
treef5f90cbbd3be7d2a3fbad35e0712a136521ea882
parentaa38f1440497880ba9f5933f83d5a8c13d162185 (diff)
Use GLib option parsing instead of getopt
-rw-r--r--src/main.c54
1 files changed, 28 insertions, 26 deletions
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();
}