-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.c
318 lines (281 loc) · 7.26 KB
/
unix.c
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
/*
* Unix main loop for QEmacs
*
* Copyright (c) 2002, 2003 Fabrice Bellard.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#ifdef CONFIG_WIN32
#include <winsock.h>
/* Use a conditional typedef to avoid compilation warning */
typedef u_int fdesc_t;
#else
#include <sys/wait.h>
typedef int fdesc_t;
#endif
/* NOTE: it is strongly inspirated from the 'links' browser API */
typedef struct URLHandler {
void *read_opaque;
void (*read_cb)(void *opaque);
void *write_opaque;
void (*write_cb)(void *opaque);
} URLHandler;
typedef struct PidHandler {
struct PidHandler *next, *prev;
int pid;
void (*cb)(void *opaque, int status);
void *opaque;
} PidHandler;
typedef struct BottomHalfEntry {
struct BottomHalfEntry *next, *prev;
void (*cb)(void *opaque);
void *opaque;
} BottomHalfEntry;
struct QETimer {
void *opaque;
void (*cb)(void *opaque);
int timeout;
struct QETimer *next;
};
static fd_set url_rfds, url_wfds;
static int url_fdmax;
static URLHandler url_handlers[256];
static int url_exit_request;
static LIST_HEAD(pid_handlers);
static LIST_HEAD(bottom_halves);
static QETimer *first_timer;
void set_read_handler(int fd, void (*cb)(void *opaque), void *opaque)
{
url_handlers[fd].read_cb = cb;
url_handlers[fd].read_opaque = opaque;
if (cb) {
if (fd >= url_fdmax)
url_fdmax = fd;
FD_SET((fdesc_t)fd, &url_rfds);
} else {
FD_CLR((fdesc_t)fd, &url_rfds);
}
}
void set_write_handler(int fd, void (*cb)(void *opaque), void *opaque)
{
url_handlers[fd].write_cb = cb;
url_handlers[fd].write_opaque = opaque;
if (cb) {
if (fd >= url_fdmax)
url_fdmax = fd;
FD_SET((fdesc_t)fd, &url_wfds);
} else {
FD_CLR((fdesc_t)fd, &url_wfds);
}
}
/* register a callback which is called when process 'pid'
terminates. When the callback is set to NULL, it is deleted */
/* XXX: add consistency check ? */
int set_pid_handler(int pid,
void (*cb)(void *opaque, int status), void *opaque)
{
PidHandler *p;
if (cb == NULL) {
list_for_each(p, &pid_handlers) {
if (p->pid == pid) {
list_del(p);
qe_free(&p);
break;
}
}
} else {
p = qe_malloc(PidHandler);
if (!p)
return -1;
p->pid = pid;
p->cb = cb;
p->opaque = opaque;
list_add(p, &pid_handlers);
}
return 0;
}
/*
* add an explicit call back to avoid recursions
*/
void register_bottom_half(void (*cb)(void *opaque), void *opaque)
{
BottomHalfEntry *bh;
/* Should not fail */
bh = qe_malloc(BottomHalfEntry);
bh->cb = cb;
bh->opaque = opaque;
list_add(bh, &bottom_halves);
}
/*
* remove bottom half
*/
void unregister_bottom_half(void (*cb)(void *opaque), void *opaque)
{
BottomHalfEntry *bh, *bh1;
list_for_each_safe(bh, bh1, &bottom_halves) {
if (bh->cb == cb && bh->opaque == opaque) {
list_del(bh);
qe_free(&bh);
}
}
}
QETimer *qe_add_timer(int delay, void *opaque, void (*cb)(void *opaque))
{
QETimer *ti;
ti = qe_malloc(QETimer);
if (!ti)
return NULL;
ti->timeout = get_clock_ms() + delay;
ti->opaque = opaque;
ti->cb = cb;
ti->next = first_timer;
first_timer = ti;
return ti;
}
void qe_kill_timer(QETimer *ti)
{
QETimer **pt;
pt = &first_timer;
while (*pt != NULL) {
if (*pt == ti) {
*pt = ti->next;
qe_free(&ti);
} else {
pt = &(*pt)->next;
}
}
}
/* execute stacked bottom halves */
static void __call_bottom_halves(void)
{
BottomHalfEntry *bh;
while (!list_empty(&bottom_halves)) {
bh = (BottomHalfEntry *)bottom_halves.prev;
list_del(bh);
bh->cb(bh->opaque);
qe_free(&bh);
}
}
static inline void call_bottom_halves(void)
{
if (!list_empty(&bottom_halves))
__call_bottom_halves();
}
/* call timer callbacks and compute maximum next call time to
check_timers() */
static inline int check_timers(int max_delay)
{
QETimer *ti, **pt;
int timeout, cur_time;
cur_time = get_clock_ms();
timeout = cur_time + max_delay;
pt = &first_timer;
for (;;) {
ti = *pt;
if (ti == NULL)
break;
if ((ti->timeout - cur_time) <= 0) {
/* timer expired : suppress it from timer list and call callback */
*pt = ti->next;
/* warning: a new timer can be added in the callback */
ti->cb(ti->opaque);
qe_free(&ti);
call_bottom_halves();
} else {
if ((ti->timeout - timeout) < 0)
timeout = ti->timeout;
pt = &ti->next;
}
}
return timeout - cur_time;
}
static void url_block_reset(void)
{
FD_ZERO(&url_rfds);
FD_ZERO(&url_wfds);
url_fdmax = -1;
url_exit_request = 0;
}
#define MAX_DELAY 500
/* block until one event */
static void url_block(void)
{
URLHandler *uh;
int ret, i, delay;
fd_set rfds, wfds;
struct timeval tv;
delay = check_timers(MAX_DELAY);
#if 0
{
static int count;
printf("%5d: delay=%d\n", count++, delay);
}
#endif
tv.tv_sec = delay / 1000;
tv.tv_usec = (delay % 1000) * 1000;
rfds = url_rfds;
wfds = url_wfds;
ret = select(url_fdmax + 1, &rfds, &wfds, NULL, &tv);
/* call each handler */
if (ret > 0) {
uh = url_handlers;
for (i = 0;i <= url_fdmax; i++) {
if (FD_ISSET(i, &rfds)) {
uh->read_cb(uh->read_opaque);
call_bottom_halves();
}
if (FD_ISSET(i, &wfds)) {
uh->write_cb(uh->write_opaque);
call_bottom_halves();
}
uh++;
}
}
#ifndef CONFIG_WIN32
/* handle terminated children */
for (;;) {
int pid, status;
PidHandler *ph, *ph1;
if (list_empty(&pid_handlers))
break;
pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0)
break;
list_for_each_safe(ph, ph1, &pid_handlers) {
if (ph->pid == pid) {
ph->cb(ph->opaque, status);
call_bottom_halves();
break;
}
}
}
#endif
}
void url_main_loop(void (*init)(void *opaque), void *opaque)
{
url_block_reset();
init(opaque);
for (;;) {
if (url_exit_request)
break;
url_block();
}
}
/* exit from url loop */
void url_exit(void)
{
url_exit_request = 1;
}