Discussion:
[perl #37700] [TODO] Changing Default STDOUT/STDERR Filehandles for PIR Code
(too old to reply)
Chromatic
2008-12-16 23:52:49 UTC
Permalink
The simple solution is to add opcodes for 'setstdin', 'setstdout', and
'setstderr' that change the interpreter's stored FileHandle PMCs to a
PMC passed in as an argument. This will not effect any C code that
directly calls the low-level read/write functions instead of using the
Parrot_io_* functions, but then C code should pretty much always use the
Parrot_io_* functions. (Some exceptions to the general rule apply, like
when the code specifically needs to override any user changes to
STD[IN|OUT|ERR]).
Do these need to be opcodes? Can they be methods on the interpreter instead?

-- c
NotFound
2009-01-28 00:26:01 UTC
Permalink
Post by Chromatic
The simple solution is to add opcodes for 'setstdin', 'setstdout', and
'setstderr' that change the interpreter's stored FileHandle PMCs to a
PMC passed in as an argument. This will not effect any C code that
directly calls the low-level read/write functions instead of using the
Parrot_io_* functions, but then C code should pretty much always use the
Parrot_io_* functions. (Some exceptions to the general rule apply, like
when the code specifically needs to override any user changes to
STD[IN|OUT|ERR]).
Do these need to be opcodes? Can they be methods on the interpreter instead?
Here is patch:

Index: src/pmc/parrotinterpreter.pmc
===================================================================
--- src/pmc/parrotinterpreter.pmc (revision 36072)
+++ src/pmc/parrotinterpreter.pmc (working copy)
@@ -30,6 +30,8 @@
#include "parrot/dynext.h"
#include "pmc_class.h"

+#include "../io/io_private.h"
+
/*

=item C<void
@@ -718,7 +720,17 @@
Parrot_register_HLL_type(INTERP, hll_id, core_type_id, hll_type_id);
}

+ METHOD stdhandle(INTVAL fileno, PMC *newhandle :optional) {
+ PMC * handle = PMCNULL;
+ if (fileno >= PIO_STDIN_FILENO && fileno <= PIO_STDERR_FILENO) {
+ handle = interp->piodata->table[fileno];
+ if (!PMC_IS_NULL(newhandle))
+ interp->piodata->table[fileno] = newhandle;
+ }
+ RETURN(PMC *handle);
+ }

+
}

/*


And a test:

$ cat handle.pir
.sub main
.local pmc interp, nullout, stdout

interp = getinterp
nullout = open '/dev/null', 'w'
stdout = interp.'stdhandle'(1, nullout)
stdout.'print'("Hello\n")
say 'Hi'
interp.'stdhandle'(1, stdout)
say 'Bye'
.end
$ ./parrot handle.pir
Hello
Bye
--
Salu2
NotFound
2009-02-02 23:45:11 UTC
Permalink
Method added in r36305, created TT #264 about it.
--
Salu2
Loading...