diff options
author | Fabio Mascarenhas <mascarenhas@lambda-2.local> | 2010-01-21 15:12:31 -0200 |
---|---|---|
committer | Fabio Mascarenhas <mascarenhas@lambda-2.local> | 2010-01-21 15:12:31 -0200 |
commit | 47fea74335776d0d05de6225fcb3a8baa33e7f01 (patch) | |
tree | cd4d7896bc6b87ae8e6148007a753e86dc430b9b /src/bin | |
parent | 208f150e363188b617123d4d0e1406ced5d93ba3 (diff) | |
download | luarocks-47fea74335776d0d05de6225fcb3a8baa33e7f01.tar.gz luarocks-47fea74335776d0d05de6225fcb3a8baa33e7f01.tar.bz2 luarocks-47fea74335776d0d05de6225fcb3a8baa33e7f01.zip |
mingw32 build target
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/rclauncher.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/bin/rclauncher.c b/src/bin/rclauncher.c new file mode 100644 index 00000000..4ff23e4a --- /dev/null +++ b/src/bin/rclauncher.c | |||
@@ -0,0 +1,118 @@ | |||
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_pushliteral(L, "debug"); | ||
39 | lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */ | ||
40 | lua_pushliteral(L, "traceback"); | ||
41 | lua_gettable(L, -2); | ||
42 | err_func = lua_gettop (L); | ||
43 | err = luaL_loadstring (L, lua_string); | ||
44 | if(!err) { | ||
45 | int i; | ||
46 | for(i = 1; i < argc; i++) | ||
47 | lua_pushstring(L, argv[i]); | ||
48 | return lua_pcall (L, argc - 1, LUA_MULTRET, err_func); | ||
49 | } else return err; | ||
50 | } | ||
51 | |||
52 | static DWORD GetModulePath( HINSTANCE hInst, LPTSTR pszBuffer, DWORD dwSize ) | ||
53 | // | ||
54 | // Return the size of the path in bytes. | ||
55 | { | ||
56 | DWORD dwLength = GetModuleFileName( hInst, pszBuffer, dwSize ); | ||
57 | if( dwLength ) | ||
58 | { | ||
59 | while( dwLength && pszBuffer[ dwLength ] != '.' ) | ||
60 | { | ||
61 | dwLength--; | ||
62 | } | ||
63 | |||
64 | if( dwLength ) | ||
65 | pszBuffer[ dwLength ] = '\000'; | ||
66 | } | ||
67 | return dwLength; | ||
68 | } | ||
69 | |||
70 | |||
71 | /* | ||
72 | ** MAIN | ||
73 | */ | ||
74 | int main (int argc, char *argv[]) { | ||
75 | char name[ MAX_PATH ]; | ||
76 | DWORD dwLength; | ||
77 | int size; | ||
78 | luaL_Buffer b; | ||
79 | int i; | ||
80 | #ifdef UNICODE | ||
81 | TCHAR lua_wstring[4098]; | ||
82 | #endif | ||
83 | char lua_string[4098]; | ||
84 | lua_State *L = lua_open(); | ||
85 | (void)argc; /* avoid "unused parameter" warning */ | ||
86 | luaL_openlibs(L); | ||
87 | dwLength = GetModulePath( NULL, name, MAX_PATH ); | ||
88 | if(dwLength) { /* Optional bootstrap */ | ||
89 | strcat(name, ".lua"); | ||
90 | if(!luaL_loadfile (L, name)) { | ||
91 | if(lua_pcall (L, 0, LUA_MULTRET, 0)) { | ||
92 | report (L); | ||
93 | lua_close (L); | ||
94 | return EXIT_FAILURE; | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | luaL_buffinit(L, &b); | ||
99 | for(i = 1; ; i++) { | ||
100 | #ifdef UNICODE | ||
101 | size = LoadString(GetModuleHandle(NULL), i, lua_wstring, | ||
102 | sizeof(lua_string)/sizeof(TCHAR)); | ||
103 | if(size > 0) wcstombs(lua_string, lua_wstring, size + 1); | ||
104 | #else | ||
105 | size = LoadString(GetModuleHandle(NULL), i, lua_string, | ||
106 | sizeof(lua_string)/sizeof(char)); | ||
107 | #endif | ||
108 | if(size) luaL_addlstring(&b, lua_string, size); else break; | ||
109 | } | ||
110 | luaL_pushresult(&b); | ||
111 | if (runlua (L, lua_tostring(L, -1), argc, argv)) { | ||
112 | report (L); | ||
113 | lua_close (L); | ||
114 | return EXIT_FAILURE; | ||
115 | } | ||
116 | lua_close (L); | ||
117 | return EXIT_SUCCESS; | ||
118 | } | ||