lua-users home
lua-l archive

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


I think you can simply modify stdin and stdout like you mentionned, but in a dll loaded by Lua. Example:

// binstd.c ///////////////////////////////////
#include <lua.h>

LUA_API int luaopen_binstd(lua_State* L)
{
    _setmode(fileno(stdin), O_BINARY);
    _setmode(fileno(stdout), O_BINARY);
    return 0;
}
///////////////////////////////////////////////
Compile binstd.c to binstd.so or binstd.dll depending on your platform.

Test from a shell:
% cat bingrep1.lua
while true do
   local line = io.read("*l")
   if line:find((...)) then
      io.write(line.."\n")
   end
end
% lua -l binstd bingrep1.lua foo < input.bin > output.bin

With a handmade or embedded interpreter you have to explicitly load the module in the script:
% cat bingrep2.lua
require "binstd" -- This single additionnal line is required

while true do
   local line = io.read("*l")
   if line:find((...)) then
      io.write(line.."\n")
   end
end
% lua bingrep2.lua foo < input.bin > output.bin

These two scripts will run on a stock Lua interpreter, provided the binstd.(dll|so) file is in Lua's CPATH.


-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de lua@gavinmckenzie.fastmail.fm
Envoyé : 23 octobre 2006 18:05
À : Lua list
Objet : Re: A method to set stdin and stdout to binary mode?

Thanks so much for the response.

I'll start looking into learning about how to extend or integrate Lua.
I suppose I probably have two choices? ...

a) extend Lua with C functions that return binary-mode file handles for stdin and stdout

b) call my Lua program from C and pass in binary-mode stdin and stdout

I'm assuming that when i crack open the lua header files I'll find the typedef for lua file handles, yes?

Sorry for the newbie questions.

Best Regards,

Gavin.

 
On Mon, 23 Oct 2006 18:50:08 -0300, "Luiz Henrique de Figueiredo"
<lhf@tecgraf.puc-rio.br> said:
> >   _setmode(fileno(stdin), O_BINARY);
> >   _setmode(fileno(stdout), O_BINARY);
> > 
> > So...I'm looking for a way to access stdin and stdout in binary mode. 
> > Is there any way to make this happen in Lua?
> 
> Not from stock Lua. You need to add external C code that provides this.
> A simple C library built as a DLL suffice if you cannot build your own 
> Lua interpreter that has those two lines.
> --lhf