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
|
/* Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees.
See file COPYING. */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <errno.h> /* for errno, (POSIX?/ANSI) */
#include <string.h> /* FD_ZERO sometimes needs this */
#include <locale.h> /* ISO C99 */
#include "sysdep.h"
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include "c-mods.h"
#include "scheme48vm.h"
#include "event.h"
#include "fd-io.h"
#include "unix.h"
/* Non-blocking I/O on file descriptors.
There appear to be two ways to get non-blocking input and output. One
is to open files with the O_NONBLOCK flag (and to use fcntl() to do the
same to stdin and stdout), the other is to call select() on each file
descriptor before doing the I/O operation. O_NONBLOCK has the problem
of being a property of the file descriptor, and its use with stdin and
stdout can lead to horrible results.
We use a mixture of both. For input files we call select() before doing
a read(), because read() will return immediately if there are any bytes
available at all, and using O_NONBLOCK on stdin is a very bad idea.
Output files are opened using O_NONBLOCK and stdout is left alone.
*/
int
ps_open_fd(char *filename, psbool is_input, long *status)
{
#define FILE_NAME_SIZE 1024
#define PERMISSION 0666 /* read and write for everyone */
char filename_temp[FILE_NAME_SIZE];
char *expanded;
extern char *s48_expand_file_name(char *, char *, int);
int flags;
mode_t mode;
expanded = s48_expand_file_name(filename, filename_temp, FILE_NAME_SIZE);
if (expanded == NULL)
return -1;
if (is_input) {
flags = O_RDONLY;
mode = 0; }
else {
flags = O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK;
mode = PERMISSION; }
/* keep trying if interrupted */
while(1) {
int fd = open(expanded, flags, mode);
if (fd != -1) {
*status = NO_ERRORS;
return fd; }
else if (errno != EINTR) {
*status = errno;
return -1; }
}
}
int
ps_close_fd(long fd_as_long)
{
int fd = (int)fd_as_long;
/* keep retrying if interrupted */
while(1) {
int status = close(fd);
if (status != -1) {
s48_remove_fd(fd);
return NO_ERRORS; }
else if (errno != EINTR)
return errno;
}
}
psbool ps_check_fd(long fd_as_long, psbool is_read, long *status)
{
int fd = (int)fd_as_long;
int ready;
#ifdef HAVE_POLL
struct pollfd pollfds[1];
pollfds[0].fd = fd;
pollfds[0].events = is_read? POLLIN : POLLOUT;
*status = NO_ERRORS;
while(1) {
ready = poll(pollfds, 1, 0);
#else /* not HAVE_POLL */
struct timeval timeout;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
timerclear(&timeout);
*status = NO_ERRORS;
while(1) {
ready = select(fd + 1,
is_read ? &fds : NULL,
is_read ? NULL : &fds,
&fds,
&timeout);
#endif /* not HAVE_POLL */
if (ready == 0)
return PSFALSE;
/* Mike has witnessed it return 2 on Mac OS X. */
else if (ready >= 1)
return PSTRUE;
else if (errno != EINTR) {
*status = errno;
return PSFALSE; } }
}
long
ps_read_fd(long fd_as_long, char *buffer, long max, psbool waitp,
psbool *eofp, psbool *pending, long *status)
{
int got, ready;
void *buf = (void *)buffer;
int fd = (int)fd_as_long;
#ifdef HAVE_POLL
struct pollfd pollfds[1];
pollfds[0].fd = fd;
pollfds[0].events = POLLIN;
/* for the normal return */
*eofp = PSFALSE;
*pending = PSFALSE;
*status = NO_ERRORS;
while(1) {
ready = poll(pollfds, 1, 0);
#else /* not HAVE_POLL */
struct timeval timeout;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
timerclear(&timeout);
/* for the normal return */
*eofp = PSFALSE;
*pending = PSFALSE;
*status = NO_ERRORS;
while(1) {
ready = select(fd + 1, &readfds, NULL, &readfds, &timeout);
#endif /* not HAVE_POLL */
if (ready == 0) {
if (!waitp)
return 0;
else if (s48_add_pending_fd(fd, PSTRUE)) {
*pending = PSTRUE;
return 0; }
else {
*status = ENOMEM; /* as close as POSIX gets */
return 0; }}
else if (ready == -1) {
if (errno != EINTR) {
*status = errno;
return 0; } }
else { /* characters waiting */
got = read(fd, buf, max);
if (got > 0) { /* all is well */
return got; }
else if (got == 0) { /* end of file */
*eofp = PSTRUE;
return 0; }
else if (errno == EINTR) { /* HCC */
return 0; }
else if (errno == EAGAIN) { /* HCC */
if (!waitp)
return 0;
else if (s48_add_pending_fd(fd, PSTRUE)) {
*pending = PSTRUE;
return 0; }
else {
*status = ENOMEM; /* as close as POSIX gets */
return 0; } }
else {
*status = errno;
return 0; } } }
}
long
ps_write_fd(long fd_as_long, char *buffer, long max, psbool *pending, long *status)
{
int sent;
int fd = (int)fd_as_long;
void *buf = (void *)buffer;
*pending = PSFALSE;
*status = NO_ERRORS;
sent = write(fd, buf, max);
if (sent > 0)
{}
else if (errno == EINTR || errno == EAGAIN) { /* HCC */
if (s48_add_pending_fd(fd, PSFALSE))
*pending = PSTRUE;
else
*status = ENOMEM; /* as close as POSIX gets */
sent = 0; }
else {
*status = errno;
sent = 0; }
return sent;
}
long
ps_io_buffer_size(void)
{
return 4096;
}
psbool
ps_io_crlf_p(void)
{
return PSFALSE;
}
char *
ps_console_encoding(long fd_as_long)
{
static char *encoding_STDIN = NULL;
static char *encoding_STDOUT = NULL;
static char *encoding_STDERR = NULL;
static char setlocale_called = PSFALSE;
char *codeset;
char** encoding_p;
if (fd_as_long == STDIN_FD())
encoding_p = &encoding_STDIN;
else if (fd_as_long == STDOUT_FD())
encoding_p = &encoding_STDOUT;
else if (fd_as_long == STDERR_FD())
encoding_p = &encoding_STDERR;
/* Mike has no clue what the rationale for needing this is. */
if (!setlocale_called)
{
setlocale(LC_CTYPE, "");
setlocale_called = PSTRUE;
}
if (*encoding_p == NULL)
{
codeset = nl_langinfo(CODESET); /* this ain't reentrant */
*encoding_p = malloc(strlen(codeset) + 1);
if (*encoding_p == NULL)
return NULL;
strcpy(*encoding_p, codeset);
}
return *encoding_p;
}
long
ps_abort_fd_op(long fd_as_long)
{
int fd = (int)fd_as_long;
if (!s48_remove_fd(fd))
fprintf(stderr, "Error: ps_abort_fd_op, no pending operation on fd %d\n",
fd);
return 0; /* because we do not actually do any I/O in parallel the
status is always zero: no characters transfered. */
}
/*
* This checks that fd's destined for output channels are marked
* as nonblocking. Stdout and stderr are a special case because
* we do not want to screw up the shell, if we were invoked from
* one.
*/
s48_value
s48_add_channel(s48_value mode, s48_value id, long fd)
{
if (mode == S48_CHANNEL_STATUS_OUTPUT
&& fd != 1
&& fd != 2) {
int flags;
RETRY_OR_RAISE_NEG(flags, fcntl(fd, F_GETFL));
if ((flags & O_NONBLOCK) == 0)
fprintf(stderr,
"Warning: output channel file descriptor %d is not non-blocking\n",
(int) fd); }
return s48_really_add_channel(mode, id, fd);
}
|