61059364aad03e0bc0757e8fa92cc4e1b0e3fa60
[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 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <stdarg.h>
20 #include <syslog.h>
21 #include "../cokebank.h"
22
23 // === IMPORTS ===
24 extern void     Init_Handlers(void);
25 extern void     Load_Itemlist(void);
26 extern void     Server_Start(void);
27 extern int      giServer_Port;
28 extern char     *gsItemListFile;
29 extern char     *gsCoke_SerialPort;
30 extern char     *gsSnack_SerialPort;
31 extern char     *gsDoor_Password;
32
33 // === GLOBALS ===
34  int    giDebugLevel = 0;
35 char    *gsCokebankPath = "cokebank.db";
36
37 // === CODE ===
38 void sigint_handler()
39 {
40         exit(0);
41 }
42
43 int main(int argc, char *argv[])
44 {
45          int    i;
46         
47         // Parse Arguments
48         for( i = 1; i < argc; i++ )
49         {
50                 char    *arg = argv[i];
51                 if( arg[0] == '-' && arg[1] != '-')
52                 {
53                         switch(arg[1])
54                         {
55                         case 'p':
56                                 if( i + 1 >= argc )     return -1;
57                                 giServer_Port = atoi(argv[++i]);
58                                 break;
59                         case 'd':
60                                 if( i + 1 >= argc )     return -1;
61                                 giDebugLevel = atoi(argv[++i]);
62                                 break;
63                         default:
64                                 // Usage Error?
65                                 break;
66                         }
67                 }
68                 else if( arg[0] == '-' && arg[1] == '-' ) {
69                         if( strcmp(arg, "--itemsfile") == 0 ) {
70                                 if( i + 1 >= argc )     return -1;
71                                 gsItemListFile = argv[++i];
72                         }
73                         else if( strcmp(arg, "--cokeport") == 0 ) {
74                                 if( i + 1 >= argc )     return -1;
75                                 gsCoke_SerialPort = argv[++i];
76                         }
77                         else if( strcmp(arg, "--snackport") == 0 ) {
78                                 if( i + 1 >= argc )     return -1;
79                                 gsSnack_SerialPort = argv[++i];
80                         }
81                         else if( strcmp(arg, "--doorpass") == 0 ) {
82                                 if( i + 1 >= argc )     return -1;
83                                 gsDoor_Password = argv[++i];
84                         }
85                         else {
86                                 // Usage error?
87                         }
88                 }
89                 else {
90                         // Usage Error?
91                 }
92         }
93         
94         signal(SIGINT, sigint_handler);
95         
96         openlog("odispense2", 0, LOG_LOCAL4);
97         
98         if( Bank_Initialise(gsCokebankPath) )
99                 return -1;
100
101         Init_Handlers();
102
103         Load_Itemlist();
104         
105         Server_Start();
106         
107
108         return 0;
109 }
110
111 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
112 {
113          int    ret;
114         
115         ret = regexec(regex, string, nMatches, matches, 0);
116         if( ret == REG_NOMATCH ) {
117                 return -1;
118         }
119         if( ret ) {
120                 size_t  len = regerror(ret, regex, NULL, 0);
121                 char    errorStr[len];
122                 regerror(ret, regex, errorStr, len);
123                 printf("string = '%s'\n", string);
124                 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
125                 exit(-1);
126         }
127         
128         return ret;
129 }
130
131 void CompileRegex(regex_t *regex, const char *pattern, int flags)
132 {
133          int    ret = regcomp(regex, pattern, flags);
134         if( ret ) {
135                 size_t  len = regerror(ret, regex, NULL, 0);
136                 char    errorStr[len];
137                 regerror(ret, regex, errorStr, len);
138                 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
139                 exit(-1);
140         }
141 }
142
143 // Serial helper
144 int InitSerial(const char *File, int BaudRate)
145 {
146         struct termios  info;
147          int    baud;
148          int    fd;
149         
150         fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
151         if( fd == -1 )  return -1;
152         
153         switch(BaudRate)
154         {
155         case 9600:      baud = B9600;   break;
156         default:        close(fd);      return -1;
157         }
158         
159         info.c_lflag = 0;       // Non-Canoical, No Echo
160         info.c_cflag = baud | CS8 | CLOCAL | CREAD;     // baud, 8N1
161         cfsetspeed(&info, baud);
162         info.c_cc[VTIME] = 0;   // No time limit
163         info.c_cc[VMIN] = 1;    // Block until 1 char
164         
165         tcflush(fd, TCIFLUSH);
166         tcsetattr(fd, TCSANOW, &info);
167         
168         return fd;
169 }
170
171
172 /**
173  * \brief Create a formatted heap string
174  */
175 char *mkstr(const char *Format, ...)
176 {
177         va_list args;
178          int    len;
179         char    *ret;
180
181         va_start(args, Format);
182         len = vsnprintf(NULL, 0, Format, args);
183         va_end(args);
184
185         ret = malloc( len + 1 );
186         if(!ret)        return NULL;
187
188         va_start(args, Format);
189         vsprintf(ret, Format, args);
190         va_end(args);
191         
192         return ret;
193 }
194

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