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;
34 char *gsCokebankPath = "cokebank.db";
42 int main(int argc, char *argv[])
47 for( i = 1; i < argc; i++ )
50 if( arg[0] == '-' && arg[1] != '-')
55 giServer_Port = atoi(argv[++i]);
58 giDebugLevel = atoi(argv[++i]);
65 else if( arg[0] == '-' && arg[1] == '-' ) {
66 if( strcmp(arg, "--itemsfile") == 0 ) {
67 gsItemListFile = argv[++i];
69 else if( strcmp(arg, "--cokeport") == 0 ) {
70 gsCoke_SerialPort = argv[++i];
72 else if( strcmp(arg, "--snackport") == 0 ) {
73 gsSnack_SerialPort = argv[++i];
84 signal(SIGINT, sigint_handler);
86 openlog("odispense2", 0, LOG_LOCAL4);
88 if( Bank_Initialise(gsCokebankPath) )
101 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
105 ret = regexec(regex, string, nMatches, matches, 0);
106 if( ret == REG_NOMATCH ) {
110 size_t len = regerror(ret, regex, NULL, 0);
112 regerror(ret, regex, errorStr, len);
113 printf("string = '%s'\n", string);
114 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
121 void CompileRegex(regex_t *regex, const char *pattern, int flags)
123 int ret = regcomp(regex, pattern, flags);
125 size_t len = regerror(ret, regex, NULL, 0);
127 regerror(ret, regex, errorStr, len);
128 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
134 int InitSerial(const char *File, int BaudRate)
140 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
141 if( fd == -1 ) return -1;
145 case 9600: baud = B9600; break;
146 default: close(fd); return -1;
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
155 tcflush(fd, TCIFLUSH);
156 tcsetattr(fd, TCSANOW, &info);
163 * \brief Create a formatted heap string
165 char *mkstr(const char *Format, ...)
171 va_start(args, Format);
172 len = vsnprintf(NULL, 0, Format, args);
175 ret = malloc( len + 1 );
176 if(!ret) return NULL;
178 va_start(args, Format);
179 vsprintf(ret, Format, args);