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.
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;
33 char *gsCokebankPath = "cokebank.db";
41 int main(int argc, char *argv[])
46 for( i = 1; i < argc; i++ )
49 if( arg[0] == '-' && arg[1] != '-')
54 giServer_Port = atoi(argv[++i]);
57 giDebugLevel = atoi(argv[++i]);
64 else if( arg[0] == '-' && arg[1] == '-' ) {
65 if( strcmp(arg, "--itemsfile") == 0 ) {
66 gsItemListFile = argv[++i];
68 else if( strcmp(arg, "--cokeport") == 0 ) {
69 gsCoke_SerialPort = argv[++i];
71 else if( strcmp(arg, "--snackport") == 0 ) {
72 gsSnack_SerialPort = argv[++i];
83 signal(SIGINT, sigint_handler);
85 Init_Cokebank(gsCokebankPath);
97 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
101 ret = regexec(regex, string, nMatches, matches, 0);
103 size_t len = regerror(ret, regex, NULL, 0);
105 regerror(ret, regex, errorStr, len);
106 printf("string = '%s'\n", string);
107 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
114 void CompileRegex(regex_t *regex, const char *pattern, int flags)
116 int ret = regcomp(regex, pattern, flags);
118 size_t len = regerror(ret, regex, NULL, 0);
120 regerror(ret, regex, errorStr, len);
121 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
127 int InitSerial(const char *File, int BaudRate)
133 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
134 if( fd == -1 ) return -1;
138 case 9600: baud = B9600; break;
139 default: close(fd); return -1;
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
148 tcflush(fd, TCIFLUSH);
149 tcsetattr(fd, TCSANOW, &info);
156 * \brief Create a formatted heap string
158 char *mkstr(const char *Format, ...)
164 va_start(args, Format);
165 len = vsnprintf(NULL, 0, Format, args);
168 ret = malloc( len + 1 );
169 if(!ret) return NULL;
171 va_start(args, Format);
172 vsprintf(ret, Format, args);