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

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