-
Notifications
You must be signed in to change notification settings - Fork 0
/
extio.cc
280 lines (253 loc) · 5.32 KB
/
extio.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <unistd.h>
//#include <sys/wait.h>
#include <unistd.h>
#include <curses.h>
#include <stdlib.h>
#include "woorden.h"
#include "bord.h"
#include "extio.h"
int prog1pid, prog2pid;
int prog1output[2], prog1input[2], prog2output[2], prog2input[2];
int prog1error[2], prog2error[2];
char prog1buf[256];
int prog1bufptr;
char prog2buf[256];
int prog2bufptr;
char letters[48];
int prog1_pos[48];
int prog2_pos[48];
char prog1_bord[49];
char prog2_bord[49];
char errormsg[256];
struct timeval left1, left2;
void bordtoprog1(void)
{
int i=0;
for (i=0; i<49; i++) prog1_bord[i]=bord[i];
}
void bordtoprog2(void)
{
int i=0;
for (i=0; i<49; i++) prog2_bord[i]=bord[i];
}
void prog1tobord(void)
{
int i=0;
for (i=0; i<49; i++) bord[i]=prog1_bord[i];
}
void prog2tobord(void)
{
int i=0;
for (i=0; i<49; i++) bord[i]=prog2_bord[i];
}
int read_from_pipedes(int filedes[2], char *buf, int max, timeval *left)
{
int n;
fd_set rfds;
struct timeval tv, start, stop;
int retval;
FD_ZERO(&rfds);
FD_SET(filedes[0], &rfds);
tv=*left;
// tv.tv_sec = 10;
// tv.tv_usec = 0;
gettimeofday(&start, NULL);
retval=select(filedes[0]+1, &rfds, NULL, NULL, &tv);
gettimeofday(&stop, NULL);
left->tv_usec -= (stop.tv_usec-start.tv_usec);
if (left->tv_usec<0)
{
left->tv_sec--;
left->tv_usec+=1000000;
}
left->tv_sec -= (stop.tv_sec-start.tv_sec);
if (left->tv_sec<0) return -2;
if (retval)
{
n=read(filedes[0], buf, max-1);
if (n<0) return -1;
buf[n]='\0';
}
else return -2;
return n;
}
int read_from_pipedes_line(int *bufptr, char *buffer, int filedes[2], char *buf, int max, timeval *left)
{
int i, n;
if (*bufptr==-1) // firstuse
{
n=read_from_pipedes(filedes, buffer, max, left);
if (n<0) return n;
*bufptr=0;
}
else if (buffer[*bufptr]=='\0')
{
n=read_from_pipedes(filedes, buffer, max, left);
if (n<0) return n;
*bufptr=0;
}
else (*bufptr)++;
n=-1;
if (buffer[*bufptr]=='\0' || buffer[*bufptr]=='\n')
{
n=read_from_pipedes(filedes, buffer, max, left);
if (n<0) return n;
*bufptr=0;
}
for (i=*bufptr; i<max; i++)
{
if (buffer[i]=='\n') { n=i; break; }
else if (buffer[i]=='\0') { n=i; break; }
}
if (n==-1) n=max-1;
for (i=*bufptr; i<n; i++)
{
buf[i-*bufptr]=buffer[i];
}
buf[i-*bufptr]='\0';
n-=*bufptr;
*bufptr+=n;
return n;
}
int write_to_pipedes(int filedes[2], char *buf)
{
int i=0, j, n;
n=strlen(buf);
while (i<n)
{
j=write(filedes[1], buf+i, n-i);
if (j<0) return j;
i+=j;
}
return n;
}
void write_prog1(char *buf, int *error)
{
if (write_to_pipedes(prog1input, buf)<0)
{
strcpy(errormsg, "Error writing to pipe program 1...\n");
*error=1;
}
}
void write_prog2(char *buf, int *error)
{
if (write_to_pipedes(prog2input, buf)<0)
{
strcpy(errormsg, "Error writing to pipe program 2...\n");
*error=2;
}
}
void read_prog2(char *buf, int *error)
{
int n;
if (*error) return;
n=read_from_pipedes_line(&prog2bufptr, prog2buf, prog2output, buf, 256, &left2);
if (n==-1)
{
strcpy(errormsg, "Error reading from pipe to program 2...\n");
*error=2;
}
else if (n==-2)
{
strcpy(errormsg, "Timeout program 2...\n");
*error=2;
}
}
void read_prog1(char *buf, int *error)
{
int n;
if (*error) return;
n=read_from_pipedes_line(&prog1bufptr, prog1buf, prog1output, buf, 256, &left1);
if (n==-1)
{
strcpy(errormsg, "Error reading from pipe to program 1...\n");
*error=1;
}
else if (n==-2)
{
strcpy(errormsg, "Timeout program 1...\n");
*error=1;
}
}
void wread_prog2(WINDOW *scr, int x, char *buf, int *error)
{
int n;
if (*error) return;
n=read_from_pipedes_line(&prog2bufptr, prog2buf, prog2output, buf, 256, &left2);
if (n==-1)
{
strcpy(errormsg, "Error reading from pipe to program 2...\n");
*error=2;
}
else if (n==-2)
{
strcpy(errormsg, "Timeout program 2...\n");
*error=2;
}
mvwprintw(scr, 0, x, "%d.%d\n", left2.tv_sec, left2.tv_usec);
wrefresh(scr);
}
void wread_prog1(WINDOW *scr, int x, char *buf, int *error)
{
int n;
if (*error) return;
n=read_from_pipedes_line(&prog1bufptr, prog1buf, prog1output, buf, 256, &left1);
if (n==-1)
{
strcpy(errormsg, "Error reading from pipe to program 1...\n");
*error=1;
}
else if (n==-2)
{
strcpy(errormsg, "Timeout program 1...\n");
*error=1;
}
mvwprintw(scr, 0, x, "%d.%d\n", left1.tv_sec, left1.tv_usec);
wrefresh(scr);
}
void read_write_reset(void)
{
prog1bufptr=-1;
prog2bufptr=-1;
left1.tv_sec=30;
left1.tv_usec=0;
left2.tv_sec=30;
left2.tv_usec=0;
}
void getprinterror(int i, int filedes[2], WINDOW *scr, bool log, char *logfile)
{
int n;
int retval;
fd_set rfds;
struct timeval tv;
char errorbuf[32768];
FD_ZERO(&rfds);
FD_SET(filedes[0], &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
retval=select(filedes[0]+1, &rfds, NULL, NULL, &tv);
if (retval)
{
n=read(filedes[0], errorbuf, 32767);
if (n<0) return;
errorbuf[n]='\0';
}
else return;
if (scr!=NULL) {
wprintw(scr, "%s", errorbuf);
wrefresh(scr);
}
if (log)
{
FILE *logstr=fopen(logfile, "a");
fprintf(logstr, "%s", errorbuf);
fclose(logstr);
}
}