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

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