/* OpenCOBOL Lua interface */ /* tectonics: cobc -c -I/usr/include/lua50 oclua.c */ #include #include /* Include the Lua API header files. */ #include #include #include static lua_State *oclua_state; int main(void) { const char *retstr; /* Declare the Lua libraries we wish to use. */ /* Note: If you are opening and running a file containing Lua code */ /* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */ /* used in that file here also. */ /* static */ const luaL_reg lualibs[] = { { "base", luaopen_base }, { "math", luaopen_math }, { NULL, NULL } }; /* A function to open up all the Lua libraries you declared above. */ /* static */ void openlualibs(lua_State *l) { const luaL_reg *lib; for (lib = lualibs; lib->func != NULL; lib++) { lib->func(l); lua_settop(l, 0); } } /* Declare a Lua State, open the Lua State and load the libraries (see above). */ /* lua_State *l; */ oclua_state = lua_open(); openlualibs(oclua_state); /* You can do what you want here. Note: Remember to update the libraries used (see above) */ /* if you add to your program and use new Lua libraries. */ /* In the lines below, I load and run the Lua code contained in the file */ /* "script.lua". */ /* Plus print some text directly from C. */ printf("This line in directly from C\n\n"); lua_dofile(oclua_state, "script.lua"); printf("\nBack to C again\n\n"); // lua_getglobal(oclua_state, "hello"); int i; for (i = lua_gettop(oclua_state); i--; i > 0) { retstr = lua_tostring(oclua_state, i); printf("To string %d: %s\n", i+1, retstr); } /* Remember to destroy the Lua State */ lua_close(oclua_state); return 0; }