From fb486758f15667a782b89fc06039c40d52e50ac0 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 8 Jan 2011 01:13:20 +0100 Subject: [PATCH] [io] Unify stdhandle init code --- include/parrot/io_unix.h | 6 +++++ include/parrot/io_win32.h | 6 +++++ src/io/core.c | 13 ++++++++- src/io/unix.c | 55 +++++++++++++++++++++++---------------- src/io/win32.c | 50 +++++++++++++++++++++-------------- 5 files changed, 88 insertions(+), 42 deletions(-) diff --git a/include/parrot/io_unix.h b/include/parrot/io_unix.h index 451eb576e1..6ac3a99399 100644 --- a/include/parrot/io_unix.h +++ b/include/parrot/io_unix.h @@ -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) @@ -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)) @@ -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) \ diff --git a/include/parrot/io_win32.h b/include/parrot/io_win32.h index f076020789..0f9f4468c9 100644 --- a/include/parrot/io_win32.h +++ b/include/parrot/io_win32.h @@ -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); @@ -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)) @@ -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) \ diff --git a/src/io/core.c b/src/io/core.c index 8317367ca7..10a17a9d0f 100644 --- a/src/io/core.c +++ b/src/io/core.c @@ -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"); diff --git a/src/io/unix.c b/src/io/unix.c index 8bbed3b17e..080f0d2f3f 100644 --- a/src/io/unix.c +++ b/src/io/unix.c @@ -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 + +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 + +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 Returns C.