File: fd.c

package info (click to toggle)
ytalk 3.0.3-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 320 kB
  • ctags: 522
  • sloc: ansic: 5,764; makefile: 170
file content (287 lines) | stat: -rw-r--r-- 6,043 bytes parent folder | download | duplicates (2)
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
/* fd.c */

/*			   NOTICE
 *
 * Copyright (c) 1990,1992,1993 Britt Yenne.  All rights reserved.
 * 
 * This software is provided AS-IS.  The author gives no warranty,
 * real or assumed, and takes no responsibility whatsoever for any 
 * use or misuse of this software, or any damage created by its use
 * or misuse.
 * 
 * This software may be freely copied and distributed provided that
 * no part of this NOTICE is deleted or edited in any manner.
 * 
 */

/* Mail comments or questions to ytalk@austin.eds.com */

#include "header.h"
#include "menu.h"
#include <sys/time.h>
#include <signal.h>
#ifdef _AIX
# include <sys/select.h>
#endif

static fd_set fdset;		/* descriptors to select on */
static fd_set fdtmp;		/* descriptors to select on (input_loop) */
static fd_set sel;		/* currently readable descriptors */
static int high_fd = 0;		/* highest fd so far */
int input_flag = 0;		/* flag: waiting for user input */
int user_winch = 0;		/* flag: user window/status changed */

struct fd_func {
    void (*func)();			/* user function */
};
static struct fd_func tag[MAX_FILES];	/* one function per file descriptor */

/* Initialize fdset data.
 */
void
init_fd()
{
    FD_ZERO(&fdset);
}

/* Add a file descriptor to the current checklist.  The supplied
 * function will be called whenever the file descriptor has input
 * waiting.
 */
void
add_fd(fd, user_func)
  int fd;
  void (*user_func)();
{
    if(fd < 0 || fd >= MAX_FILES)
    {
	show_error("add_fd: descriptor out of range");
	return;
    }
    FD_SET(fd, &fdset);
    tag[fd].func = user_func;
    if(fd >= high_fd)
	high_fd = fd + 1;
}

/* Remove a file descriptor from the checklist.
 */
void
remove_fd(fd)
  int fd;
{
    if(fd < 0 || fd >= MAX_FILES)
    {
	show_error("remove_fd: descriptor out of range");
	return;
    }
    FD_CLR(fd, &fdset);
    FD_CLR(fd, &fdtmp);
    FD_CLR(fd, &sel);
}

/* Read an entire length of data.
 * Returns 0 on success, -1 on error.
 */
int
full_read(fd, buf, len)
  int fd;
  register char *buf;
  register int len;
{
    register int rc;

    while(len > 0)
    {
	if((rc = read(fd, buf, len)) <= 0)
	    return -1;
	buf += rc;
	len -= rc;
    }
    return 0;
}

/* -- MAIN LOOPS -- */

static ylong lastping, curtime;

void
main_loop()
{
    register int fd, rc;
    struct timeval tv;
#ifndef Y_USE_SIGHOLD
    int mask, old_mask;
#endif

    /* Some signals need to be blocked while doing internal
     * processing, else some craziness might occur.
     */

#ifndef Y_USE_SIGHOLD

    mask = 0;

# ifdef SIGWINCH
    mask |= sigmask(SIGWINCH);
# endif

#endif

#if defined(SIGCHLD)
    signal(SIGCHLD, SIG_IGN);
#else
# if defined(SIGCLD)
    signal(SIGCLD, SIG_IGN);
# endif
#endif

    /* For housecleaning to occur every CLEAN_INTERVAL seconds, we make
     * our own little timer system.  SIGALRM is nice; in fact it's so
     * useful that we'll be using it in other parts of YTalk.  Since
     * we therefore can't use it here, we affect the timer manually.
     */

    house_clean();
    curtime = lastping = (ylong)time(NULL);
    for(;;)
    {
	/* check if we're done */

	if(connect_list == NULL
	&& wait_list == NULL
	&& menu_ptr == NULL
	&& running_process == 0)
	    bail(0);

	/* select */

	sel = fdset;
	if(curtime > lastping + CLEAN_INTERVAL)
	    tv.tv_sec = 0;
	else
	    tv.tv_sec = (lastping + CLEAN_INTERVAL) - curtime;
	tv.tv_usec = 0;
	if((rc = select(high_fd, &sel, 0, 0, &tv)) < 0)
	    if(errno != EINTR)
		show_error("main_loop: select failed");

	/* block signals while doing internal processing */

#ifdef Y_USE_SIGHOLD
# ifdef SIGWINCH
	sighold(SIGWINCH);
# endif
#else
	old_mask = sigblock(mask);
#endif

	/* process file descriptors with input waiting */

	if(rc > 0)
	    for(fd = 0; fd < high_fd; fd++)
		if(FD_ISSET(fd, &sel))
		{
		    errno = 0;
		    tag[fd].func(fd);
		    if(--rc <= 0)
			break;
		}

	/* check timer */

	curtime = (ylong)time(NULL);
	if(curtime - lastping >= CLEAN_INTERVAL)
	{
	    house_clean();
	    lastping = (ylong)time(NULL);
	}

	/* re-allow signals */

#ifdef Y_USE_SIGHOLD
# ifdef SIGWINCH
	sigrelse(SIGWINCH);
# endif
#else
	sigsetmask(old_mask);
#endif
	if(user_winch)
	{
	    /* This is a cute hack that updates a user menu
	     * dynamically as information changes.  So I had
	     * some free time.  there.
	     */
	    user_winch = 0;
	    update_user_menu();
	}
    }
}

/* Input loop.  This loop keeps everything except user input going until
 * input is received from <me>.  This is necessary for answering pressing
 * questions without needing to add a getch_term() function to the terminal
 * definition library.  Hack?  maybe.  Fun, tho.
 */
void
input_loop()
{
    register int fd, rc;
    struct timeval tv;
    static int left_loop;

    left_loop = 0;
    fdtmp = fdset;
    while(io_len <= 0)
    {
	/* select */

	sel = fdtmp;
	if(curtime > lastping + CLEAN_INTERVAL)
	    tv.tv_sec = 0;
	else
	    tv.tv_sec = (lastping + CLEAN_INTERVAL) - curtime;
	tv.tv_usec = 0;
	if((rc = select(high_fd, &sel, 0, 0, &tv)) < 0)
	    if(errno != EINTR)
		show_error("input_loop: select failed");

	/* process file descriptors with input waiting */

	if(rc > 0)
	    for(fd = 0; fd < high_fd; fd++)
		if(FD_ISSET(fd, &sel))
		{
		    /* Here the hack begins.  Any function that takes user
		     * input should clear "input_flag" and return.  This
		     * tells us to ignore this function for now.  Any
		     * function which receives input from <me> should leave
		     * my input in io_ptr/io_len.
		     */
		    errno = 0;
		    input_flag = 1;
		    tag[fd].func(fd);
		    if(left_loop) /* recursive input_loop()s.  argh! */
			return;   /* let my parent function re-call me */
		    if(input_flag == 0)
		    {
			/* don't check this descriptor anymore */
			FD_CLR(fd, &fdtmp);
		    }
		    if(--rc <= 0)
			break;
		}

	/* check timer */

	curtime = (ylong)time(NULL);
	if(curtime - lastping >= CLEAN_INTERVAL)
	{
	    input_flag = 1;
	    house_clean();
	    lastping = (ylong)time(NULL);
	}
    }
    input_flag = 0;
    left_loop = 1;
}