From c8ca9622fe25f936881889e95d60a6a91126ff13 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 12 Sep 2006 23:54:26 +0000 Subject: [PATCH] Configuration file may be specified in command line The configuration filename may be specified in command line, which is parsed using GOptionContext. --- improxy.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/improxy.c b/improxy.c index 987ecb0..4217a51 100644 --- a/improxy.c +++ b/improxy.c @@ -3,6 +3,8 @@ #include #include "log.h" +#define CONFFILE "/etc/improxy.conf" + void client_event (GConn* conn, GConnEvent* event, gpointer data) { GConn* server; @@ -48,9 +50,19 @@ void new_client (GServer* server, GConn* conn, gpointer data) gnet_conn_read (conn); } -int main () +static gchar* configfile; + +static GOptionEntry opt_entries[] = + { + { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile, + "Configuration file location", "file" }, + { NULL } + }; + +int main (int argc, char** argv) { + GOptionContext* opt_ctx; GKeyFile *keyfile; GInetAddr* inetaddr; gchar* conf_address; @@ -59,9 +71,19 @@ int main () gnet_init (); im_log_init (); + configfile = CONFFILE; + opt_ctx = g_option_context_new (""); + g_option_context_add_main_entries (opt_ctx, opt_entries, NULL); + if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL)) + { + g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL, + "Could not parse command line options."); + } + g_option_context_free (opt_ctx); + keyfile = g_key_file_new (); - g_key_file_load_from_file (keyfile, "improxy.conf", G_KEY_FILE_NONE, NULL); + g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL); conf_address = g_key_file_get_string (keyfile, "global", "address", NULL); port = g_key_file_get_integer (keyfile, "global", "port", NULL); -- 2.20.1