Fixing coke handler and login (a nice cleanup)
[tpg/opendispense2.git] / src / server / main.c
index e789b13..ac6dd27 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <signal.h>
 #include "common.h"
 
 // === IMPORTS ===
-extern void    Init_Cokebank(void);    // cokebank.c
+extern void    Init_Cokebank(const char *Argument);    // cokebank.c
+extern void    Init_Handlers(void);
 extern void    Load_Itemlist(void);
 extern void    Server_Start(void);
 extern int     giServer_Port;
 extern char*   gsItemListFile;
 extern char*   gsCoke_SerialPort;
+extern char*   gsSnack_SerialPort;
 
 // === GLOBALS ===
  int   giDebugLevel = 0;
+char   *gsCokebankPath = "cokebank.db";
 
 // === CODE ===
+void sigint_handler()
+{
+       exit(0);
+}
+
 int main(int argc, char *argv[])
 {
         int    i;
@@ -51,9 +60,12 @@ int main(int argc, char *argv[])
                        if( strcmp(arg, "--itemsfile") == 0 ) {
                                gsItemListFile = argv[++i];
                        }
-                       if( strcmp(arg, "--cokeport") == 0 ) {
+                       else if( strcmp(arg, "--cokeport") == 0 ) {
                                gsCoke_SerialPort = argv[++i];
                        }
+                       else if( strcmp(arg, "--snackport") == 0 ) {
+                               gsSnack_SerialPort = argv[++i];
+                       }
                        else {
                                // Usage error?
                        }
@@ -63,12 +75,47 @@ int main(int argc, char *argv[])
                }
        }
        
-       Init_Cokebank();
+       signal(SIGINT, sigint_handler);
        
+       Init_Cokebank(gsCokebankPath);
+
+       Init_Handlers();
+
        Load_Itemlist();
        
        Server_Start();
        
+
        return 0;
 }
 
+int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
+{
+        int    ret;
+       
+       ret = regexec(regex, string, nMatches, matches, 0);
+       if( ret ) {
+               size_t  len = regerror(ret, regex, NULL, 0);
+               char    errorStr[len];
+               regerror(ret, regex, errorStr, len);
+               printf("string = '%s'\n", string);
+               fprintf(stderr, "%s\n%s", errorMessage, errorStr);
+               exit(-1);
+       }
+       
+       return ret;
+}
+
+void CompileRegex(regex_t *regex, const char *pattern, int flags)
+{
+        int    ret = regcomp(regex, pattern, flags);
+       if( ret ) {
+               size_t  len = regerror(ret, regex, NULL, 0);
+               char    errorStr[len];
+               regerror(ret, regex, errorStr, len);
+               fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
+               exit(-1);
+       }
+}
+
+

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