a903f752aaea03ccee30403e3649e05a70eb72fd
[tpg/opendispense2.git] / src / server / main.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * main.c - Initialisation Code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <signal.h>
14 #include "common.h"
15 #include <termios.h>
16
17 // === IMPORTS ===
18 extern void     Init_Cokebank(const char *Argument);    // cokebank.c
19 extern void     Init_Handlers(void);
20 extern void     Load_Itemlist(void);
21 extern void     Server_Start(void);
22 extern int      giServer_Port;
23 extern char*    gsItemListFile;
24 extern char*    gsCoke_SerialPort;
25 extern char*    gsSnack_SerialPort;
26
27 // === GLOBALS ===
28  int    giDebugLevel = 0;
29 char    *gsCokebankPath = "cokebank.db";
30
31 // === CODE ===
32 void sigint_handler()
33 {
34         exit(0);
35 }
36
37 int main(int argc, char *argv[])
38 {
39          int    i;
40         
41         // Parse Arguments
42         for( i = 1; i < argc; i++ )
43         {
44                 char    *arg = argv[i];
45                 if( arg[0] == '-' && arg[1] != '-')
46                 {
47                         switch(arg[1])
48                         {
49                         case 'p':
50                                 giServer_Port = atoi(argv[++i]);
51                                 break;
52                         case 'd':
53                                 giDebugLevel = atoi(argv[++i]);
54                                 break;
55                         default:
56                                 // Usage Error?
57                                 break;
58                         }
59                 }
60                 else if( arg[0] == '-' && arg[1] == '-' ) {
61                         if( strcmp(arg, "--itemsfile") == 0 ) {
62                                 gsItemListFile = argv[++i];
63                         }
64                         else if( strcmp(arg, "--cokeport") == 0 ) {
65                                 gsCoke_SerialPort = argv[++i];
66                         }
67                         else if( strcmp(arg, "--snackport") == 0 ) {
68                                 gsSnack_SerialPort = argv[++i];
69                         }
70                         else {
71                                 // Usage error?
72                         }
73                 }
74                 else {
75                         // Usage Error?
76                 }
77         }
78         
79         signal(SIGINT, sigint_handler);
80         
81         Init_Cokebank(gsCokebankPath);
82
83         Init_Handlers();
84
85         Load_Itemlist();
86         
87         Server_Start();
88         
89
90         return 0;
91 }
92
93 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
94 {
95          int    ret;
96         
97         ret = regexec(regex, string, nMatches, matches, 0);
98         if( ret ) {
99                 size_t  len = regerror(ret, regex, NULL, 0);
100                 char    errorStr[len];
101                 regerror(ret, regex, errorStr, len);
102                 printf("string = '%s'\n", string);
103                 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
104                 exit(-1);
105         }
106         
107         return ret;
108 }
109
110 void CompileRegex(regex_t *regex, const char *pattern, int flags)
111 {
112          int    ret = regcomp(regex, pattern, flags);
113         if( ret ) {
114                 size_t  len = regerror(ret, regex, NULL, 0);
115                 char    errorStr[len];
116                 regerror(ret, regex, errorStr, len);
117                 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
118                 exit(-1);
119         }
120 }
121
122 // Serial helper
123 void InitSerial(int FD, int BaudRate)
124 {
125         struct termios  info;
126          int    baud;
127         
128         switch(BaudRate)
129         {
130         case 9600:      baud = B9600;   break;
131         default:        return ;
132         }
133         
134         cfmakeraw(&info);       // Sets 8N1
135         cfsetspeed(&info, baud);
136         
137         tcsetattr(FD, TCSANOW, &info);
138 }
139
140

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