Low-ish level MacRuby interaction
Howdy folks, I'm working with MacRuby as it evolves and I've been enjoying it quite a lot. I'm trying to make use of the system in an unusual way and not having much luck. I thought I would post here and see if anyone has suggestions. What I'm trying to do is use MacRuby as an embedded scripting language. Using the framework and the Objective-C interface this is REALLY straightforward and easy. What I'd like to do, however, is divorce the MacRuby standard streams (stdout, stdio, stderr) so that I can control them myself. I was able to do this in a rather straightforward fashion from the Ruby side by, for example, reopening STDOUT on the write end of an NSPipe that I create myself. The issue I've run into is that the NSPipe doesn't appear to grab the output from Ruby in the same way that original STDOUT does. I've been reading a lot and I gather that the difference may have something to do with having STDOUT attached to a "controlling terminal" vs. going to the pipe. I've been swimming around in a world of pseudo terminals and pipes and lots of UNIX-ish stuff that, while it is teaching me a lot, is not getting me much closer to my goal. So, my question is can anyone suggest the "best" way I might be able to hijack the standard streams that are being used by the Ruby runtime so that I can forward ruby output to my own code? Scott
Hi Scott! Here is a code snippet that hijacks stdout using an NSPipe. The idea is to first initialize the MacRuby VM (so that the standard descriptor objects are created), then use dup2(2), then evaluate your Ruby expressions. #import <Foundation/Foundation.h> #import <MacRuby/MacRuby.h> int main(void) { MacRuby *vm = [MacRuby sharedRuntime]; NSPipe *pipe = [NSPipe pipe]; dup2([[pipe fileHandleForWriting] fileDescriptor], 1); [vm evaluateString:@"p 42"]; NSData *data = [[pipe fileHandleForReading] availableData]; NSLog(@"data from pipe: %@", data); return 0; } $ gcc t.m -o t -framework Foundation -framework MacRuby -fobjc-gc - arch x86_64 $ ./t 2009-06-10 17:57:35.322 t[96349:10b] data from pipe: <34320a> HTH, Laurent & Ben! On Jun 10, 2009, at 5:04 PM, Scott Thompson wrote:
Howdy folks,
I'm working with MacRuby as it evolves and I've been enjoying it quite a lot. I'm trying to make use of the system in an unusual way and not having much luck. I thought I would post here and see if anyone has suggestions.
What I'm trying to do is use MacRuby as an embedded scripting language. Using the framework and the Objective-C interface this is REALLY straightforward and easy.
What I'd like to do, however, is divorce the MacRuby standard streams (stdout, stdio, stderr) so that I can control them myself. I was able to do this in a rather straightforward fashion from the Ruby side by, for example, reopening STDOUT on the write end of an NSPipe that I create myself.
The issue I've run into is that the NSPipe doesn't appear to grab the output from Ruby in the same way that original STDOUT does. I've been reading a lot and I gather that the difference may have something to do with having STDOUT attached to a "controlling terminal" vs. going to the pipe.
I've been swimming around in a world of pseudo terminals and pipes and lots of UNIX-ish stuff that, while it is teaching me a lot, is not getting me much closer to my goal.
So, my question is can anyone suggest the "best" way I might be able to hijack the standard streams that are being used by the Ruby runtime so that I can forward ruby output to my own code?
Scott
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (2)
-
Laurent Sansonetti
-
Scott Thompson