lua-users home
lua-l archive

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


Paul Moore <pf_moore <at> yahoo.co.uk> writes:

> What I'd like to do is to make the Lua concept of standard IO point at 
> different (FILE *)'s, in such a way that all Lua IO (print, io.stdin, 
> error messages, etc, etc) goes to my own handles. Is there a way of 
> doing this?
[...]
> Oddly, it appears that io.output() doesn't affect print, otherwise that 
> would be an option. Maybe I could set io.output and override print - but 
> how can I be sure I've caught everything?
> 
> Can anyone offer any suggestions? I've searched the mailing list 
> archives, and was surprised to find no hints there.

Actually, after grepping the lua source for references to stdin/out/err, I 
came to the conclusion that it's not too hard to do this. I believe I need:

* Don't use luaL_dofile with a NULL file, as that's hard-coded to use stdin.
* Set up my own panic handler to replace the standard one that writes to 
stderr.
* Override the print builtin.
* Don't expose the debug library debug() function, or override it.
* Replace io.stdin/out/err.

Not too bad.

The big issue is replacing the io library handles from C. The io library 
doesn't have a C API, so I don't see a way of taking a C FILE* and using it as 
the standard io input/output. In effect, I'd need the equivalent of the 
following horrible mix of Lua and C:

    void setup(FILE *in, FILE *out, FILE *err) {
        io.stdin = createluafile(in);
        io.stdout = createluafile(in);
        io.stderr = createluafile(in);
        io.input(io.stdin);
        io.output(io.stdout);
    }

Short of something like this, I suspect I may just have to reimplement the 
whole of the io library (which doesn't make me happy :-()

Paul.