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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* exp_event.c - event interface for Expect
Written by: Don Libes, NIST, 2/6/90
I hereby place this software in the public domain. However, the author and
NIST would appreciate credit if this program or parts of it are used.
*/
#include "expect_cf.h"
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_PTYTRAP
# include <sys/ptyio.h>
#endif
#include "tcl.h"
#include "exp_prog.h"
#include "exp_command.h" /* for ExpState defs */
#include "exp_event.h"
typedef struct ThreadSpecificData {
int rr; /* round robin ptr */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
void
exp_event_disarm_bg(esPtr)
ExpState *esPtr;
{
Tcl_DeleteChannelHandler(esPtr->channel,exp_background_channelhandler,(ClientData)esPtr);
}
static void
exp_arm_background_channelhandler_force(esPtr)
ExpState *esPtr;
{
Tcl_CreateChannelHandler(esPtr->channel,
TCL_READABLE|TCL_EXCEPTION,
exp_background_channelhandler,
(ClientData)esPtr);
esPtr->bg_status = armed;
}
void
exp_arm_background_channelhandler(esPtr)
ExpState *esPtr;
{
switch (esPtr->bg_status) {
case unarmed:
exp_arm_background_channelhandler_force(esPtr);
break;
case disarm_req_while_blocked:
esPtr->bg_status = blocked; /* forget request */
break;
case armed:
case blocked:
/* do nothing */
break;
}
}
void
exp_disarm_background_channelhandler(esPtr)
ExpState *esPtr;
{
switch (esPtr->bg_status) {
case blocked:
esPtr->bg_status = disarm_req_while_blocked;
break;
case armed:
esPtr->bg_status = unarmed;
exp_event_disarm_bg(esPtr);
break;
case disarm_req_while_blocked:
case unarmed:
/* do nothing */
break;
}
}
/* ignore block status and forcibly disarm handler - called from exp_close. */
/* After exp_close returns, we will not have an opportunity to disarm */
/* because the fd will be invalid, so we force it here. */
void
exp_disarm_background_channelhandler_force(esPtr)
ExpState *esPtr;
{
switch (esPtr->bg_status) {
case blocked:
case disarm_req_while_blocked:
case armed:
esPtr->bg_status = unarmed;
exp_event_disarm_bg(esPtr);
break;
case unarmed:
/* do nothing */
break;
}
}
/* this can only be called at the end of the bg handler in which */
/* case we know the status is some kind of "blocked" */
void
exp_unblock_background_channelhandler(esPtr)
ExpState *esPtr;
{
switch (esPtr->bg_status) {
case blocked:
exp_arm_background_channelhandler_force(esPtr);
break;
case disarm_req_while_blocked:
exp_disarm_background_channelhandler_force(esPtr);
break;
}
}
/* this can only be called at the beginning of the bg handler in which */
/* case we know the status must be "armed" */
void
exp_block_background_channelhandler(esPtr)
ExpState *esPtr;
{
esPtr->bg_status = blocked;
exp_event_disarm_bg(esPtr);
}
/*ARGSUSED*/
static void
exp_timehandler(clientData)
ClientData clientData;
{
*(int *)clientData = TRUE;
}
static void exp_channelhandler(clientData,mask)
ClientData clientData;
int mask;
{
ExpState *esPtr = (ExpState *)clientData;
esPtr->notified = TRUE;
esPtr->notifiedMask = mask;
exp_event_disarm_fg(esPtr);
}
void
exp_event_disarm_fg(esPtr)
ExpState *esPtr;
{
/*printf("DeleteChannelHandler: %s\r\n",esPtr->name);*/
Tcl_DeleteChannelHandler(esPtr->channel,exp_channelhandler,(ClientData)esPtr);
/* remember that ChannelHandler has been disabled so that */
/* it can be turned on for fg expect's as well as bg */
esPtr->fg_armed = FALSE;
}
/* returns status, one of EOF, TIMEOUT, ERROR or DATA */
/* can now return RECONFIGURE, too */
/*ARGSUSED*/
int exp_get_next_event(interp,esPtrs,n,esPtrOut,timeout,key)
Tcl_Interp *interp;
ExpState *(esPtrs[]);
int n; /* # of esPtrs */
ExpState **esPtrOut; /* 1st ready esPtr, not set if none */
int timeout; /* seconds */
int key;
{
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
ExpState *esPtr;
int i; /* index into in-array */
#ifdef HAVE_PTYTRAP
struct request_info ioctl_info;
#endif
int old_configure_count = exp_configure_count;
int timerFired = FALSE;
Tcl_TimerToken timerToken = 0;/* handle to Tcl timehandler descriptor */
/* We must delete any timer before returning. Doing so throughout
* the code makes it unreadable; isolate the unreadable nonsense here.
*/
#define RETURN(x) { \
if (timerToken) Tcl_DeleteTimerHandler(timerToken); \
return(x); \
}
for (;;) {
/* if anything has been touched by someone else, report that */
/* an event has been received */
for (i=0;i<n;i++) {
tsdPtr->rr++;
if (tsdPtr->rr >= n) tsdPtr->rr = 0;
esPtr = esPtrs[tsdPtr->rr];
if (esPtr->key != key) {
esPtr->key = key;
esPtr->force_read = FALSE;
*esPtrOut = esPtr;
RETURN(EXP_DATA_OLD);
} else if ((!esPtr->force_read) && (!expSizeZero(esPtr))) {
*esPtrOut = esPtr;
RETURN(EXP_DATA_OLD);
} else if (esPtr->notified) {
/* this test of the mask should be redundant but SunOS */
/* raises both READABLE and EXCEPTION (for no */
/* apparent reason) when selecting on a plain file */
if (esPtr->notifiedMask & TCL_READABLE) {
*esPtrOut = esPtr;
esPtr->notified = FALSE;
RETURN(EXP_DATA_NEW);
}
/*
* at this point we know that the event must be TCL_EXCEPTION
* indicating either EOF or HP ptytrap.
*/
#ifndef HAVE_PTYTRAP
RETURN(EXP_EOF);
#else
if (ioctl(esPtr->fdin,TIOCREQCHECK,&ioctl_info) < 0) {
expDiagLog("ioctl error on TIOCREQCHECK: %s", Tcl_PosixError(interp));
RETURN(EXP_TCLERROR);
}
if (ioctl_info.request == TIOCCLOSE) {
RETURN(EXP_EOF);
}
if (ioctl(esPtr->fdin, TIOCREQSET, &ioctl_info) < 0) {
expDiagLog("ioctl error on TIOCREQSET after ioctl or open on slave: %s", Tcl_ErrnoMsg(errno));
}
/* presumably, we trapped an open here */
/* so simply continue by falling thru */
#endif /* !HAVE_PTYTRAP */
}
}
if (!timerToken) {
if (timeout >= 0) {
timerToken = Tcl_CreateTimerHandler(1000*timeout,
exp_timehandler,
(ClientData)&timerFired);
}
}
/* make sure that all fds that should be armed are */
for (i=0;i<n;i++) {
esPtr = esPtrs[i];
/*printf("CreateChannelHandler: %s\r\n",esPtr->name);*/
Tcl_CreateChannelHandler(
esPtr->channel,
TCL_READABLE | TCL_EXCEPTION,
exp_channelhandler,
(ClientData)esPtr);
esPtr->fg_armed = TRUE;
}
Tcl_DoOneEvent(0); /* do any event */
if (timerFired) return(EXP_TIMEOUT);
if (old_configure_count != exp_configure_count) {
RETURN(EXP_RECONFIGURE);
}
}
}
/* Having been told there was an event for a specific ExpState, get it */
/* This returns status, one of EOF, TIMEOUT, ERROR or DATA */
/*ARGSUSED*/
int
exp_get_next_event_info(interp,esPtr)
Tcl_Interp *interp;
ExpState *esPtr;
{
#ifdef HAVE_PTYTRAP
struct request_info ioctl_info;
#endif
if (esPtr->notifiedMask & TCL_READABLE) return EXP_DATA_NEW;
/* ready_mask must contain TCL_EXCEPTION */
#ifndef HAVE_PTYTRAP
return(EXP_EOF);
#else
if (ioctl(esPtr->fdin,TIOCREQCHECK,&ioctl_info) < 0) {
expDiagLog("ioctl error on TIOCREQCHECK: %s",
Tcl_PosixError(interp));
return(EXP_TCLERROR);
}
if (ioctl_info.request == TIOCCLOSE) {
return(EXP_EOF);
}
if (ioctl(esPtr->fdin, TIOCREQSET, &ioctl_info) < 0) {
expDiagLog("ioctl error on TIOCREQSET after ioctl or open on slave: %s", Tcl_ErrnoMsg(errno));
}
/* presumably, we trapped an open here */
/* call it an error for lack of anything more descriptive */
/* it will be thrown away by caller anyway */
return EXP_TCLERROR;
#endif
}
/*ARGSUSED*/
int /* returns TCL_XXX */
exp_dsleep(interp,sec)
Tcl_Interp *interp;
double sec;
{
int timerFired = FALSE;
Tcl_CreateTimerHandler((int)(sec*1000),exp_timehandler,(ClientData)&timerFired);
while (!timerFired) {
Tcl_DoOneEvent(0);
}
return TCL_OK;
}
static char destroy_cmd[] = "destroy .";
static void
exp_event_exit_real(interp)
Tcl_Interp *interp;
{
Tcl_Eval(interp,destroy_cmd);
}
/* set things up for later calls to event handler */
void
exp_init_event()
{
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
tsdPtr->rr = 0;
exp_event_exit = exp_event_exit_real;
}
|