On Sat, 10 May 2003, ts wrote:

> >>>>> "a" == ahoward  <ahoward / fsl.noaa.gov> writes:
>
> a> now, i personally have had much better luck using #reopen, but i'm curious as
> a> to your rational.
>
>  No rational,

i doubt that ;-)

> pigeon% cat a.c
> #include <ruby.h>
>
> int main()
> {
>     int pipes[2], count;
>     VALUE params[2], w;
>     char str[512];
>     FILE *fd;
>
>     if ((fd = fopen("aaa", "w")) == NULL) {
>         exit(1);
>     }
>     if (pipe(pipes) == -1) {
>         exit(1);
>     }
>     ruby_init();
>     ruby_init_loadpath();
>
>     params[0] = INT2FIX(pipes[1]);
>     params[1] = rb_str_new2("w");
>     w = rb_class_new_instance(2, params, rb_path2class("IO"));
>
>     rb_funcall(rb_defout, rb_intern("reopen"), 1, w);
>     rb_funcall(rb_stderr, rb_intern("reopen"), 1, w);
>     rb_eval_string("$stderr.puts 'aa'; puts 'hello'; system('ls a.c xx')");
>     count = read(pipes[0], str, 512);
>     if (count >= 0) {
>         fprintf(fd, "<<\n%*.*s>>\n", count, count, str);
>     }
>     fclose(fd);
>     return 0;
> }
> pigeon%
>
> pigeon% a.out
> pigeon%
>
> pigeon% cat aaa
> <<
> aa
> hello
> ls: xx: No such file or directory
> a.c
> >>
> pigeon%

guy - you rock.

you do realize the pain we all have gone through trying to accomplish that!?
;-)

one thing which is curious to me is that simply redirecting fd 1 and 2 from
within C using dup/dup2 seems to redirect *some* of ruby's output, but not all
of it.  i hadn't considered (didn't know about) rb_defout, etc., but am a
little suprised that direct manipulation of fd 0,1,2 before calling
ruby_init() did not accomplish the task of 'complete' ruby redirection...

i guess i really don't understand the relationship between low level io (fd
0,1,2), C stdio (FILE *), and ruby's interface/use of these but it certainly
seems like it should be possible to setup ALL redirection in pure C, without
using and of the libruby methods, but i am probably wrong.

in anycase, i'll save this bit of code.


-a

--
  ====================================
  | Ara Howard
  | NOAA Forecast Systems Laboratory
  | Information and Technology Services
  | Data Systems Group
  | R/FST 325 Broadway
  | Boulder, CO 80305-3328
  | Email: ara.t.howard / fsl.noaa.gov
  | Phone:  303-497-7238
  | Fax:    303-497-7259
  ====================================