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

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