On Wed, Dec 17, 2008 at 12:52 AM, chromatic <chroma...@wgz.org> wrote:
> On Tuesday 16 December 2008 15:40:32 Allison Randal via RT wrote:
>
>> 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

Reply via email to