Client - Code to load up config file and set dispense server/port
authorMark Tearle <[email protected]>
Sat, 27 Dec 2014 14:59:56 +0000 (22:59 +0800)
committerMark Tearle <[email protected]>
Sat, 27 Dec 2014 14:59:56 +0000 (22:59 +0800)
client.conf [new file with mode: 0644]
src/client/Makefile
src/client/main.c

diff --git a/client.conf b/client.conf
new file mode 100644 (file)
index 0000000..ff657ee
--- /dev/null
@@ -0,0 +1,5 @@
+#
+# OpenDispense2 Client config file
+#
+dispense_server merlo.ucc.asn.au
+dispense_port 11021
index b70551e..5045d98 100644 (file)
@@ -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)
 
index ddf3779..4dd70df 100644 (file)
@@ -17,6 +17,7 @@
 #include <limits.h>    // 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 <min balance>\n"
                        "    -M <max balance>\n"
                        "        Set the Maximum/Minimum balances shown in `dispense acct`\n"
+                       "    -f <configfile>\n"
+                       "        Set the config file path (default: `/etc/opendispense/client.conf'\n"
                        "Definitions:\n"
                        "    <itemid>\n"
                        "        Item ID of the form <type>:<num> where <type> is a non-empty string of alpha-numeric characters, and <num> 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

UCC git Repository :: git.ucc.asn.au