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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
#include "c_defs.h"
/************************************************************************
*
* I/O library routines
*
* $Id: iolb.c,v 1.10 2004/05/08 21:01:41 jon Exp $
*
* Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
***********************************************************************/
/**********************************************************************/
/* Unix/C specific porting and supporting code Copyright (C)1994-1996 */
/* by Jon Trulson <jon@radscan.com> under the same terms and */
/* conditions of the original copyright by Jef Poskanzer and Craig */
/* Leres. */
/* */
/**********************************************************************/
#include "global.h"
#include "ibuf.h"
#include "iolb.h"
#include "cd2lb.h"
/* iochav - test whether a char is available to be read or not */
/* synopsis */
/* int avail, iochav */
/* avail = iochav() */
/* note */
/* This routine does \not/ iopeek() because of speed considerations. */
/* The two routines are meant to virtually be copies of one another and */
/* should be modified in parallel. */
int iochav( void )
{
#if defined(USE_SELECT)
/* we do a test for linux/sys_select_h
for glibc systems... for some reason
glibc defined fd_set as a typedef, rather
than as a struct like the rest of the
planet...*/
# if defined(LINUX) && defined(HAVE_SYS_SELECT_H)
/* a linux, glibc system */
static fd_set readfds;
# else
static struct fd_set readfds;
# endif
static struct timeval timeout;
#else /* poll */
static struct pollfd Stdin_pfd;
static struct strbuf CtlMsg;
static struct strbuf DataMsg;
int flagsp;
#endif
int retval;
if (iBufCount())
{
return(TRUE);
}
#if defined(USE_SELECT)
/* here, we'll just use select */
FD_ZERO(&readfds);
FD_SET(PollInputfd, &readfds);
timeout.tv_sec = 0; /* setting to zero should ret immediately */
timeout.tv_usec = 0;
if ((retval = select(PollInputfd+1, &readfds, NULL, NULL, &timeout)) == -1)
{
clog("iochav(): select(): %s", strerror(errno));
return(FALSE);
}
if (retval == 0) /* nothing there */
{
return(FALSE);
}
else
{ /* something avail */
return(TRUE);
}
#else /* use poll() */
CtlMsg.maxlen = 0;
CtlMsg.buf = NULL;
DataMsg.maxlen = 0;
DataMsg.buf = NULL;
flagsp = 0;
Stdin_pfd.fd = PollInputfd; /* stdin */
Stdin_pfd.events = (POLLIN | POLLRDNORM);
if (poll(&Stdin_pfd, 1, 0) > 0) /* return immediately if a char avail */
{
#ifdef DEBUG_IO
clog("ALTiochav(): had a char via poll");
#endif
if ((retval = getmsg(PollInputfd, &CtlMsg, &DataMsg,
&flagsp)) == -1)
{
clog("iochav(): getmsg(): failed: %s", strerror(errno));
}
else
{
#ifdef DEBUG_IO
clog("iochav(): getmsg() = %d: DataMsg.len = %d CtlMsg.len = %d",
retval,
DataMsg.len,
CtlMsg.len);
#endif
if (DataMsg.len <= 0 && retval > 0)
{
#ifdef DEBUG_IO
clog("iochav(): getmsg(): DataMsg.len <= 0 retval = %d - TRUE",
DataMsg.len,
retval);
#endif
return(TRUE);
}
if (DataMsg.len <= 0 && CtlMsg.len <= 0)
{
#ifdef DEBUG_IO
clog("iochav(): getmsg(): DataMsg.len & CtlMsg.len <= 0 ret FALSE");
#endif
return(FALSE);
}
else
{
#ifdef DEBUG_IO
clog("iochav(): getmsg(): DataMsg.len | CtlMsg.len > 0 ret TRUE");
#endif
return(TRUE);
}
}
}
else
{
#ifdef DEBUG_IO
clog("ALTiochav(): NO char via poll");
#endif
return(FALSE);
}
#endif /* !USE_SELECT */
}
/* ioeat - swallow any input that has come so far */
/* synopsis */
/* ioeat */
/* description */
/* This routines flushes the type ahead buffer. That is, it reads any */
/* characters that are available, but no more. */
void ioeat(void)
{
while ( iochav() )
iogchar();
return;
}
/* iogchar - get a character */
/* SYNOPSIS */
/* char ch, iogchar */
/* ch = iogchar ( ch ) */
int iogchar ( void )
{
static unsigned int thechar = 0;
/* This is a good place to flush the output buffer and to */
/* check for terminal broadcasts. */
cdrefresh();
timeout(-1); /* wait for a while */
reloop:
if (!iBufCount())
{
c_sleep(0.1);
thechar = wgetch(stdscr);
}
else
thechar = iBufGetCh();
if (thechar == ERR)
{
#ifdef DEBUG_IO
clog("iogchar() thechar == ERR, errno = %d", errno);
#endif
goto reloop;
}
return(thechar);
}
/* iogtimed - get a char with timeout */
/* SYNOPSIS */
/* int *ch */
/* int seconds */
/* gotone = iogtimed ( &ch, seconds ) */
/* iogtimed - get a char with timeout */
/* SYNOPSIS */
/* int *ch */
/* int seconds */
/* gotone = iogtimed ( &ch, seconds ) */
int iogtimed ( int *ch, real seconds )
{
static int c;
unsigned int secs, usecs;
#if !defined(LINUX)
time_t starttime, curtime;
#endif
#if defined(USE_SELECT)
static struct timeval timeout;
# if defined(LINUX) && defined(HAVE_SYS_SELECT_H)
/* a linux, glibc system */
static fd_set readfds;
# else
static struct fd_set readfds;
# endif
#endif
int retval;
/* This is a good place to flush the output buffer. */
cdrefresh();
if (iBufCount())
{
*ch = iBufGetCh();
return(TRUE);
}
/* compute proper secs/usecs to allow for subsecond sleeps */
/* some systems don't grok usecs >1,000,000 */
if (seconds >= 1.0)
{
secs = (int) seconds;
usecs = (int) (1000000 * (seconds - (real) secs));
}
else
{
secs = 0;
usecs = (1000000 * seconds);
}
#if defined(LINUX) && defined(USE_SELECT)
/* with linux, select returns the time
remaining if the select is interrupted,
this means we can set the timer once
and reuse it, so that the select will
sleep for whatever time is left if it's
interrupted. Don't count on this
behavior with other unix's... */
FD_ZERO(&readfds);
FD_SET(PollInputfd, &readfds);
timeout.tv_sec = secs;
timeout.tv_usec = usecs;
#endif
#if !defined(LINUX)
/* Linux will return the amount of time left in a
select() call if it is interupted, others won't,
so we need to set a timer to make sure we leave
when 'seconds' are up */
starttime = time(0);
#endif
while (TRUE)
{
errno = 0; /* reset errno */
#if defined(USE_SELECT) && !defined(LINUX)
/* if we're using select, but not on linux,
then we have to restart the whole timer
again if it's interrupted... don't know
how to get around that... */
FD_ZERO(&readfds);
FD_SET(PollInputfd, &readfds);
timeout.tv_sec = secs;
timeout.tv_usec = usecs;
#endif
#if defined(USE_SELECT)
if ((retval = select(PollInputfd + 1, &readfds, NULL, NULL,
&timeout)) == -1)
{ /* timeout or signal? */
if (errno != EINTR) /* some error */
{
*ch = 0;
clog("iogtimed(): select() failed: %s", strerror(errno));
cdrefresh();
return(FALSE);
}
else
{
# if defined(DEBUG_IOGTIMED)
clog("iogtimed(): select(): interrupted: %s",
strerror(errno));
# endif
# if !defined(LINUX)
/* determine if we've waited long enough, if so
return 0, else try again */
curtime = time(0);
if (curtime >= (starttime + secs))
{
*ch = 0;
return(FALSE);
}
else
{
continue;
}
# endif
}
}
else if (retval > 0)
{
# if defined(DEBUG_IOGTIMED)
if (FD_ISSET(PollInputfd,&readfds))
clog("After Linux select - FD_ISSET is SET");
else
clog("After Linux select - FD_ISSET is NOT SET");
clog("iogtimed(): After Linux select - errno = %d", errno);
clog("iogtimed(): After Linux select - tv_sec = %d",
timeout.tv_sec);
clog("iogtimed(): After Linux select - tv_usec = %d",
timeout.tv_usec);
# endif
c = wgetch(stdscr); /* I hope select wasn't lying... */
*ch = c;
# if defined(DEBUG_IOGTIMED)
clog("iogtimed(): retval = %d, PollInputfd = %d, *ch = %d",
retval,PollInputfd,*ch);
# endif
return(TRUE);
}
else
{
*ch = 0;
# if defined(DEBUG_IOGTIMED)
clog("iogtimed(): retval = %d, PollInputfd = %d, *ch = %d",
retval,PollInputfd,*ch);
# endif
cdrefresh();
return(FALSE);
}
#else
#error "You need the select() system call"
#endif
/* If we're here, the read was interupted, try again */
} /* while */
}
|