Skip to content

Commit

Permalink
[io] Unify stdhandle init code
Browse files Browse the repository at this point in the history
  • Loading branch information
nwellnhof committed Jan 9, 2011
1 parent 1856359 commit fb48675
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
6 changes: 6 additions & 0 deletions include/parrot/io_unix.h
Expand Up @@ -100,6 +100,9 @@ PIOOFF_T Parrot_io_seek_unix(PARROT_INTERP,
__attribute__nonnull__(2)
FUNC_MODIFIES(*filehandle);

PIOHANDLE Parrot_io_stdhandle_unix(PARROT_INTERP, INTVAL fileno)
__attribute__nonnull__(1);

PIOOFF_T Parrot_io_tell_unix(PARROT_INTERP, ARGMOD(PMC *filehandle))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
Expand Down Expand Up @@ -149,6 +152,8 @@ size_t Parrot_io_write_unix(PARROT_INTERP,
#define ASSERT_ARGS_Parrot_io_seek_unix __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(filehandle))
#define ASSERT_ARGS_Parrot_io_stdhandle_unix __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_io_tell_unix __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(filehandle))
Expand Down Expand Up @@ -266,6 +271,7 @@ INTVAL Parrot_io_socket_unix(PARROT_INTERP,


#define PIO_INIT(interp) Parrot_io_init_unix((interp))
#define PIO_STDHANDLE(interp, fileno) Parrot_io_stdhandle_unix((interp), (fileno))
#define PIO_OPEN(interp, file, flags) \
Parrot_io_open_unix((interp), (file), (flags))
#define PIO_OPEN_PIPE(interp, file, flags, pid) \
Expand Down
6 changes: 6 additions & 0 deletions include/parrot/io_win32.h
Expand Up @@ -97,6 +97,9 @@ PIOOFF_T Parrot_io_seek_win32(PARROT_INTERP,
__attribute__nonnull__(2)
FUNC_MODIFIES(*filehandle);

PIOHANDLE Parrot_io_stdhandle_win32(PARROT_INTERP, INTVAL fileno)
__attribute__nonnull__(1);

PIOOFF_T Parrot_io_tell_win32(PARROT_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
Expand Down Expand Up @@ -145,6 +148,8 @@ size_t Parrot_io_write_win32(PARROT_INTERP,
#define ASSERT_ARGS_Parrot_io_seek_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(filehandle))
#define ASSERT_ARGS_Parrot_io_stdhandle_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_io_tell_win32 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(filehandle))
Expand Down Expand Up @@ -258,6 +263,7 @@ INTVAL Parrot_io_socket_win32(PARROT_INTERP,
/* HEADERIZER END: src/io/socket_win32.c */

#define PIO_INIT(interp) Parrot_io_init_win32((interp))
#define PIO_STDHANDLE(interp, fileno) Parrot_io_stdhandle_win32((interp), (fileno))
#define PIO_OPEN(interp, file, flags) \
Parrot_io_open_win32((interp), (file), (flags))
#define PIO_OPEN_PIPE(interp, file, flags, pid) \
Expand Down
13 changes: 12 additions & 1 deletion src/io/core.c
Expand Up @@ -52,8 +52,19 @@ Parrot_io_init(PARROT_INTERP)
if (interp->piodata) {
/* memsub system is up and running: */
/* Init IO stacks and handles for interp instance. */
PIO_INIT(interp);
PIOHANDLE os_handle;

os_handle = PIO_STDHANDLE(interp, PIO_STDIN_FILENO);
_PIO_STDIN(interp) = Parrot_io_fdopen_flags(interp, PMCNULL,
os_handle, PIO_F_READ);

os_handle = PIO_STDHANDLE(interp, PIO_STDOUT_FILENO);
_PIO_STDOUT(interp) = Parrot_io_fdopen_flags(interp, PMCNULL,
os_handle, PIO_F_WRITE);

os_handle = PIO_STDHANDLE(interp, PIO_STDERR_FILENO);
_PIO_STDERR(interp) = Parrot_io_fdopen_flags(interp, PMCNULL,
os_handle, PIO_F_WRITE);

if (Interp_debug_TEST(interp, PARROT_START_DEBUG_FLAG)) {
Parrot_io_eprintf(NULL, "I/O system initialized.\n");
Expand Down
55 changes: 33 additions & 22 deletions src/io/unix.c
Expand Up @@ -102,31 +102,42 @@ INTVAL
Parrot_io_init_unix(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_io_init_unix)
ParrotIOData * const d = interp->piodata;
if (d != NULL && d->table != NULL) {
PMC *filehandle;

filehandle = Parrot_io_fdopen_flags(interp, PMCNULL, STDIN_FILENO, PIO_F_READ);
if (PMC_IS_NULL(filehandle))
return -1;
_PIO_STDIN(interp) = filehandle;

filehandle = Parrot_io_fdopen_flags(interp, PMCNULL, STDOUT_FILENO, PIO_F_WRITE);
if (PMC_IS_NULL(filehandle))
return -1;
_PIO_STDOUT(interp) = filehandle;

filehandle = Parrot_io_fdopen_flags(interp, PMCNULL, STDERR_FILENO, PIO_F_WRITE);
if (PMC_IS_NULL(filehandle))
return -1;
_PIO_STDERR(interp) = filehandle;

return 0;
}
return -1;

return 0;
}


/*
=item C<PIOHANDLE Parrot_io_stdhandle_unix(PARROT_INTERP, INTVAL fileno)>
Returns a standard file handle.
=cut
*/

PIOHANDLE
Parrot_io_stdhandle_unix(PARROT_INTERP, INTVAL fileno)
{
ASSERT_ARGS(Parrot_io_stdhandle_unix)
PIOHANDLE os_handle;

switch (fileno) {
case PIO_STDIN_FILENO:
os_handle = STDIN_FILENO;
break;
case PIO_STDOUT_FILENO:
os_handle = STDOUT_FILENO;
break;
case PIO_STDERR_FILENO:
os_handle = STDERR_FILENO;
break;
}

return os_handle;
}

/*
=item C<PIOHANDLE Parrot_io_open_unix(PARROT_INTERP, STRING *path, INTVAL
Expand Down
50 changes: 31 additions & 19 deletions src/io/win32.c
Expand Up @@ -127,28 +127,9 @@ INTVAL
Parrot_io_init_win32(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_io_init_win32)
HANDLE h;
struct WSAData sockinfo;
int ret;

if ((h = GetStdHandle(STD_INPUT_HANDLE)) != INVALID_HANDLE_VALUE) {
_PIO_STDIN(interp) = Parrot_io_fdopen_flags(interp, PMCNULL, h, PIO_F_READ);
}
else {
_PIO_STDIN(interp) = PMCNULL;
}
if ((h = GetStdHandle(STD_OUTPUT_HANDLE)) != INVALID_HANDLE_VALUE) {
_PIO_STDOUT(interp) = Parrot_io_fdopen_flags(interp, PMCNULL, h, PIO_F_WRITE);
}
else {
_PIO_STDOUT(interp) = PMCNULL;
}
if ((h = GetStdHandle(STD_ERROR_HANDLE)) != INVALID_HANDLE_VALUE) {
_PIO_STDERR(interp) = Parrot_io_fdopen_flags(interp, PMCNULL, h, PIO_F_WRITE);
}
else {
_PIO_STDERR(interp) = PMCNULL;
}
/* Start Winsock
* no idea where or whether destroy it
*/
Expand All @@ -163,6 +144,37 @@ Parrot_io_init_win32(PARROT_INTERP)

/*
=item C<PIOHANDLE Parrot_io_stdhandle_win32(PARROT_INTERP, INTVAL fileno)>
Returns a standard file handle.
=cut
*/

PIOHANDLE
Parrot_io_stdhandle_win32(PARROT_INTERP, INTVAL fileno)
{
ASSERT_ARGS(Parrot_io_stdhandle_win32)
DWORD nStdHandle;

switch (fileno) {
case PIO_STDIN_FILENO:
nStdHandle = STD_INPUT_HANDLE;
break;
case PIO_STDOUT_FILENO:
nStdHandle = STD_OUTPUT_HANDLE;
break;
case PIO_STDERR_FILENO:
nStdHandle = STD_ERROR_HANDLE;
break;
}

return GetStdHandle(nStdHandle);
}

/*
=item C<INTVAL Parrot_io_getblksize_win32(PIOHANDLE fd)>
Returns C<PIO_BLKSIZE>.
Expand Down

0 comments on commit fb48675

Please sign in to comment.