From 635a6185eb10a07051b041fa5faa0a79a5b1a989 Mon Sep 17 00:00:00 2001 From: Mark Tearle Date: Sat, 27 Dec 2014 22:59:56 +0800 Subject: [PATCH] Client - Code to load up config file and set dispense server/port --- client.conf | 5 +++++ src/client/Makefile | 2 +- src/client/main.c | 32 +++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 client.conf diff --git a/client.conf b/client.conf new file mode 100644 index 0000000..ff657ee --- /dev/null +++ b/client.conf @@ -0,0 +1,5 @@ +# +# OpenDispense2 Client config file +# +dispense_server merlo.ucc.asn.au +dispense_port 11021 diff --git a/src/client/Makefile b/src/client/Makefile index b70551e..5045d98 100644 --- a/src/client/Makefile +++ b/src/client/Makefile @@ -8,7 +8,7 @@ LDFLAGS := -g -lncurses BIN := ../../dispense OBJ := main.o protocol.o menu.o -OBJ += doregex.o +OBJ += doregex.o config.o DEPFILES := $(OBJ:%.o=%.d) diff --git a/src/client/main.c b/src/client/main.c index ddf3779..4dd70df 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -17,6 +17,7 @@ #include // INT_MIN/INT_MAX #include "common.h" #include "../common/doregex.h" +#include "../common/config.h" #define USE_NCURSES_INTERFACE 0 #define DEBUG_TRACE_SERVER 0 @@ -38,8 +39,12 @@ char *trim(char *string); void CompileRegex(regex_t *regex, const char *pattern, int flags); // === GLOBALS === -char *gsDispenseServer = "merlo.ucc.gu.uwa.edu.au"; +const char *gsConfigFile = "/etc/opendispense/client.conf"; + +const char *gsDispenseServer = "merlo.ucc.gu.uwa.edu.au"; int giDispensePort = 11020; + int giDispenseServerSet = 0; // True if set by command line + int giDispensePortSet = 0; // True if set by command line tItem *gaItems; int giNumItems; @@ -149,6 +154,8 @@ void ShowUsage(void) " -m \n" " -M \n" " Set the Maximum/Minimum balances shown in `dispense acct`\n" + " -f \n" + " Set the config file path (default: `/etc/opendispense/client.conf'\n" "Definitions:\n" " \n" " Item ID of the form : where is a non-empty string of alpha-numeric characters, and is a non-negative integer\n" @@ -604,6 +611,18 @@ int main(int argc, char *argv[]) if( ret ) return ret; + // Load config file + Config_ParseFile(gsConfigFile); + + // Parse config values + if (!giDispenseServerSet) { + gsDispenseServer = Config_GetValue("dispense_server",0); + } + if (!giDispensePortSet) { + giDispensePort = Config_GetValue_Int("dispense_port",0); + } + + // Sub-commands if( strcmp(gsTextArgs[0], "finger") == 0 ) { return subcommand_finger(); @@ -865,6 +884,15 @@ int ParseArguments(int argc, char *argv[]) giMaximumBalance = atoi(argv[++i]); break; + case 'f': // Override Config File + if( i + 1 >= argc ) { + fprintf(stderr, "%s: -f takes an argument\n", argv[0]); + ShowUsage(); + return RV_ARGUMENTS; + } + gsConfigFile = argv[++i]; + break; + case 'u': // Override User if( i + 1 >= argc ) { fprintf(stderr, "%s: -u takes an argument\n", argv[0]); @@ -881,6 +909,7 @@ int ParseArguments(int argc, char *argv[]) return RV_ARGUMENTS; } gsDispenseServer = argv[++i]; + giDispenseServerSet = 1; break; case 'P': // Override remote port if( i + 1 >= argc ) { @@ -889,6 +918,7 @@ int ParseArguments(int argc, char *argv[]) return RV_ARGUMENTS; } giDispensePort = atoi(argv[++i]); + giDispensePortSet = 1; break; // Set slot name/price -- 2.20.1