diff options
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/rclauncher.c | 139 |
1 files changed, 0 insertions, 139 deletions
diff --git a/src/bin/rclauncher.c b/src/bin/rclauncher.c deleted file mode 100644 index 60284638..00000000 --- a/src/bin/rclauncher.c +++ /dev/null | |||
| @@ -1,139 +0,0 @@ | |||
| 1 | |||
| 2 | /* | ||
| 3 | ** Simple Lua interpreter. | ||
| 4 | ** This program is used to run a Lua file embedded as a resource. | ||
| 5 | ** It creates a Lua state, opens all its standard libraries, and run | ||
| 6 | ** the Lua file in a protected environment just to redirect the error | ||
| 7 | ** messages to stdout and stderr. | ||
| 8 | ** | ||
| 9 | ** $Id: rclauncher.c,v 1.1 2008/06/30 14:29:59 carregal Exp $ | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <string.h> | ||
| 13 | #include <stdlib.h> | ||
| 14 | |||
| 15 | #include "lua.h" | ||
| 16 | #include "lauxlib.h" | ||
| 17 | #include "lualib.h" | ||
| 18 | #include <windows.h> | ||
| 19 | #include <io.h> | ||
| 20 | #include <fcntl.h> | ||
| 21 | |||
| 22 | /* | ||
| 23 | ** Report error message. | ||
| 24 | ** Assumes that the error message is on top of the stack. | ||
| 25 | */ | ||
| 26 | static int report (lua_State *L) { | ||
| 27 | fprintf (stderr, "lua: fatal error: `%s'\n", lua_tostring (L, -1)); | ||
| 28 | fflush (stderr); | ||
| 29 | printf ("Content-type: text/plain\n\nConfiguration fatal error: see error log!\n"); | ||
| 30 | printf ("%s", lua_tostring(L, -1)); | ||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | static int runlua (lua_State *L, const char *lua_string, int argc, char *argv[]) { | ||
| 35 | int err_func; | ||
| 36 | int err; | ||
| 37 | |||
| 38 | lua_getglobal(L, "debug"); | ||
| 39 | lua_pushliteral(L, "traceback"); | ||
| 40 | lua_gettable(L, -2); | ||
| 41 | err_func = lua_gettop (L); | ||
| 42 | err = luaL_loadstring (L, lua_string); | ||
| 43 | if(!err) { | ||
| 44 | // fill global arg table | ||
| 45 | lua_getglobal(L, "arg"); | ||
| 46 | int i; | ||
| 47 | for(i = 1; i < argc; i++) | ||
| 48 | { | ||
| 49 | lua_pushstring(L, argv[i]); | ||
| 50 | lua_rawseti(L, -2, i); | ||
| 51 | } | ||
| 52 | lua_pop(L, 1); | ||
| 53 | // fill parameters (in vararg '...') | ||
| 54 | for(i = 1; i < argc; i++) | ||
| 55 | lua_pushstring(L, argv[i]); | ||
| 56 | return lua_pcall (L, argc - 1, LUA_MULTRET, err_func); | ||
| 57 | } else return err; | ||
| 58 | } | ||
| 59 | |||
| 60 | static DWORD GetModulePath( HINSTANCE hInst, LPTSTR pszBuffer, DWORD dwSize ) | ||
| 61 | // | ||
| 62 | // Return the size of the path in bytes. | ||
| 63 | { | ||
| 64 | DWORD dwLength = GetModuleFileName( hInst, pszBuffer, dwSize ); | ||
| 65 | if( dwLength ) | ||
| 66 | { | ||
| 67 | while( dwLength && pszBuffer[ dwLength ] != '.' ) | ||
| 68 | { | ||
| 69 | dwLength--; | ||
| 70 | } | ||
| 71 | |||
| 72 | if( dwLength ) | ||
| 73 | pszBuffer[ dwLength ] = '\000'; | ||
| 74 | } | ||
| 75 | return dwLength; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | /* | ||
| 80 | ** MAIN | ||
| 81 | */ | ||
| 82 | int main (int argc, char *argv[]) { | ||
| 83 | char name[ MAX_PATH ]; | ||
| 84 | DWORD dwLength; | ||
| 85 | int size; | ||
| 86 | luaL_Buffer b; | ||
| 87 | int i; | ||
| 88 | #ifdef UNICODE | ||
| 89 | TCHAR lua_wstring[4098]; | ||
| 90 | #endif | ||
| 91 | char lua_string[4098]; | ||
| 92 | lua_State *L = luaL_newstate(); | ||
| 93 | (void)argc; /* avoid "unused parameter" warning */ | ||
| 94 | luaL_openlibs(L); | ||
| 95 | lua_newtable(L); // create arg table | ||
| 96 | lua_pushstring(L, argv[0]); // add interpreter to arg table | ||
| 97 | lua_rawseti(L, -2, -1); | ||
| 98 | dwLength = GetModulePath( NULL, name, MAX_PATH ); | ||
| 99 | if(dwLength) { /* Optional bootstrap */ | ||
| 100 | strcat(name, ".lua"); | ||
| 101 | lua_pushstring(L, name); // add lua script to arg table | ||
| 102 | lua_rawseti(L, -2, 0); | ||
| 103 | lua_setglobal(L,"arg"); // set global arg table | ||
| 104 | if(!luaL_loadfile (L, name)) { | ||
| 105 | if(lua_pcall (L, 0, LUA_MULTRET, 0)) { | ||
| 106 | report (L); | ||
| 107 | lua_close (L); | ||
| 108 | return EXIT_FAILURE; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | lua_pushstring(L, argv[0]); // no lua script, so add interpreter again, now as lua script | ||
| 115 | lua_rawseti(L, -2, 0); | ||
| 116 | lua_setglobal(L,"arg"); // set global arg table | ||
| 117 | } | ||
| 118 | |||
| 119 | luaL_buffinit(L, &b); | ||
| 120 | for(i = 1; ; i++) { | ||
| 121 | #ifdef UNICODE | ||
| 122 | size = LoadString(GetModuleHandle(NULL), i, lua_wstring, | ||
| 123 | sizeof(lua_string)/sizeof(TCHAR)); | ||
| 124 | if(size > 0) wcstombs(lua_string, lua_wstring, size + 1); | ||
| 125 | #else | ||
| 126 | size = LoadString(GetModuleHandle(NULL), i, lua_string, | ||
| 127 | sizeof(lua_string)/sizeof(char)); | ||
| 128 | #endif | ||
| 129 | if(size) luaL_addlstring(&b, lua_string, size); else break; | ||
| 130 | } | ||
| 131 | luaL_pushresult(&b); | ||
| 132 | if (runlua (L, lua_tostring(L, -1), argc, argv)) { | ||
| 133 | report (L); | ||
| 134 | lua_close (L); | ||
| 135 | return EXIT_FAILURE; | ||
| 136 | } | ||
| 137 | lua_close (L); | ||
| 138 | return EXIT_SUCCESS; | ||
| 139 | } | ||
