3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * main.c - Initialisation Code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
21 #include "../cokebank.h"
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;
35 char *gsCokebankPath = "cokebank.db";
43 int main(int argc, char *argv[])
48 for( i = 1; i < argc; i++ )
51 if( arg[0] == '-' && arg[1] != '-')
56 if( i + 1 >= argc ) return -1;
57 giServer_Port = atoi(argv[++i]);
60 if( i + 1 >= argc ) return -1;
61 giDebugLevel = atoi(argv[++i]);
68 else if( arg[0] == '-' && arg[1] == '-' ) {
69 if( strcmp(arg, "--itemsfile") == 0 ) {
70 if( i + 1 >= argc ) return -1;
71 gsItemListFile = argv[++i];
73 else if( strcmp(arg, "--cokeport") == 0 ) {
74 if( i + 1 >= argc ) return -1;
75 gsCoke_SerialPort = argv[++i];
77 else if( strcmp(arg, "--snackport") == 0 ) {
78 if( i + 1 >= argc ) return -1;
79 gsSnack_SerialPort = argv[++i];
81 else if( strcmp(arg, "--doorpass") == 0 ) {
82 if( i + 1 >= argc ) return -1;
83 gsDoor_Password = argv[++i];
94 signal(SIGINT, sigint_handler);
96 openlog("odispense2", 0, LOG_LOCAL4);
98 if( Bank_Initialise(gsCokebankPath) )
111 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
115 ret = regexec(regex, string, nMatches, matches, 0);
116 if( ret == REG_NOMATCH ) {
120 size_t len = regerror(ret, regex, NULL, 0);
122 regerror(ret, regex, errorStr, len);
123 printf("string = '%s'\n", string);
124 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
131 void CompileRegex(regex_t *regex, const char *pattern, int flags)
133 int ret = regcomp(regex, pattern, flags);
135 size_t len = regerror(ret, regex, NULL, 0);
137 regerror(ret, regex, errorStr, len);
138 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
144 int InitSerial(const char *File, int BaudRate)
150 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
151 if( fd == -1 ) return -1;
155 case 9600: baud = B9600; break;
156 default: close(fd); return -1;
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
165 tcflush(fd, TCIFLUSH);
166 tcsetattr(fd, TCSANOW, &info);
173 * \brief Create a formatted heap string
175 char *mkstr(const char *Format, ...)
181 va_start(args, Format);
182 len = vsnprintf(NULL, 0, Format, args);
185 ret = malloc( len + 1 );
186 if(!ret) return NULL;
188 va_start(args, Format);
189 vsprintf(ret, Format, args);