libseed-list interpreter, reading stdout



Hi,
I'm making an interpreter program that creates a gtk window with a textview for log output, and listens on stdin. Every line that comes in is appended to a buffer until ^E is encountered, then the buffer is evaluated.

I use this as a plugin for gedit, so that I can select text in a document and evaluate it in the seed interpreter while it's running, and since it's running a gtk mainloop I can use all stuff that needs this, like creating windows, setting up timers, etc "live".. (Right now it also parses the code through my AlgoScript parser, but it would be easy to bypass this for a flexible seed live-coding environment.)

Now to the problem, for print() I can simply set ctx.global.print = myprint, where the latter is a function that appends text to the log window instead. But this doesn't work in all cases, since any imported module gets the default original print function in their context again, and also any errors or warnings printed from the C code still gets to stdout.. (like g_warning())

One solution would be to use GLib.set_print_handler(), set_printerr_handler() and set_log_handler(), but these doesn't seem to work (probably needs some wrapper to call a supplied JS callback).

So instead I want to just capture stdout, which would work for everything. I know how to capture stdout from a child process, but how do I capture my own stdout? I tried stuff like this:

//var fd = os.dup(1);
var fd = 1;
var stdout = GLib.io_channel_unix_new(fd);
GLib.io_channel_set_flags(stdout,GLib.IOFlags.NONBLOCK);
GLib.io_add_watch(stdout, 0, GLib.IOCondition.IN, function(source, condition, data) {
    var x = new GLib.String;
    GLib.io_channel_read_line_string(source,x);
    _print(x.str);
    return true;
});

But all I can make it do is to redirect everything from stdin to this watcher (why this happens I don't understand), still triggering my stdin-watcher but only giving it empty strings..

/Jonatan


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]