0

I found some references on this but I was not able to make them work. I have a Debian box with mysql and mysql-proxy. I am intercepting the SQL queries with the LUA script.

function read_query(packet)
        if packet:byte() ~= proxy.COM_QUERY then
                print("error read (COM_QUERY)")
        end
    local query = packet:sub(2)
    print ("query : " .. query )
           //Transformation here
    return proxy.PROXY_SEND_QUERY
end

I want to parse and process the query so I can rewrite it with some c functions I already have developed. I am trying to find the way to call this fucntions but the only way I have found asumes that the c MAIN function starts the LUA registering process.

Is there any way to make the LUA script call the function in a compiled C file?

Any example of how should I make (LUA) and receive (C) the call?

jordi
  • 1,157
  • 1
  • 13
  • 37

2 Answers2

0

Extract from lua.org

When we say that Lua can call C functions, this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function, but they are neither portable nor robust.) ... ... for a C function to be called from Lua, we must register it, that is, we must give its address to Lua in an appropriate way.

You should have a look here

Community
  • 1
  • 1
Mali
  • 2,990
  • 18
  • 18
  • If I understand correctly I have a circular problem: to call a C function I first need to call a C function to register it. Looks like my way should be building my code as a module, isn't it?. Is there any good example or tutorial to do that? – jordi Jan 22 '14 at 17:16
  • @user2935222 Yes, if you need to call a C function from standalone Lua, you need to create a c-module or use one of the mentioned "non-robust" packages: http://www.lua.org/pil/ and http://lua-users.org/wiki/BindingCodeToLua (look for "FFI" if you want to go the "non-robust" way) – dualed Jan 23 '14 at 16:01
0

SWIG is a good option to generate bindings for you: www.swig.org. You create a .i file in which load your C headers, and SWIG generates all the binding code for you. You then compile the generated code, link it to your C library and Lua library, and in your script you put require 'yourCLibrary', you can do what you want. Very practical, and your wrapper can be used to access yourCLibrary from other languages like Python and C#.

Oliver
  • 27,510
  • 9
  • 72
  • 103