1 | /* tolua |
---|
2 | ** Support code for Lua bindings. |
---|
3 | ** Written by Waldemar Celes |
---|
4 | ** TeCGraf/PUC-Rio |
---|
5 | ** Aug 2003 |
---|
6 | ** $Id:$ |
---|
7 | ** Extension by Orxonox (Reto Grieder) to support working directory |
---|
8 | ** and direct usage of lua files. (2008) |
---|
9 | */ |
---|
10 | |
---|
11 | /* This code is free software; you can redistribute it and/or modify it. |
---|
12 | ** The software provided hereunder is on an "as is" basis, and |
---|
13 | ** the author has no obligation to provide maintenance, support, updates, |
---|
14 | ** enhancements, or modifications. |
---|
15 | */ |
---|
16 | |
---|
17 | #include "tolua++.h" |
---|
18 | |
---|
19 | #include "lua.h" |
---|
20 | #include "lualib.h" |
---|
21 | #include "lauxlib.h" |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | |
---|
28 | static void help (void) |
---|
29 | { |
---|
30 | fprintf(stderr,"\n" |
---|
31 | "usage: tolua++ [options] input_file\n" |
---|
32 | "\n" |
---|
33 | "Command line options are:\n" |
---|
34 | " -v : print version information.\n" |
---|
35 | " -o file : set output file; default is stdout.\n" |
---|
36 | " -H file : create include file.\n" |
---|
37 | " -n name : set package name; default is input file root name.\n" |
---|
38 | " -w directory : set working directory; default is location of package file.\n" |
---|
39 | " -s file : specify source lua code for the parser; all.lua is default.\n" |
---|
40 | " -p : parse only.\n" |
---|
41 | " -P : parse and print structure information (for debug).\n" |
---|
42 | " -S : disable support for c++ strings.\n" |
---|
43 | " -1 : substract 1 to operator[] index (for compatibility with tolua5).\n" |
---|
44 | " -L file : run lua file (with dofile()) before doing anything.\n" |
---|
45 | " -D : disable automatic exporting of destructors for classes that have\n" |
---|
46 | " constructors (for compatibility with tolua5)\n" |
---|
47 | " -W : disable warnings for unsupported features (for compatibility\n" |
---|
48 | " with tolua5)\n" |
---|
49 | " -C : disable cleanup of included lua code (for easier debugging)\n" |
---|
50 | " -E value[=value] : add extra values to the luastate\n" |
---|
51 | " -t : export a list of types asociates with the C++ typeid name\n" |
---|
52 | " -h : print this message.\n" |
---|
53 | "Should the input file be omitted, stdin is assumed;\n" |
---|
54 | "in that case, the package name must be explicitly set.\n\n" |
---|
55 | ); |
---|
56 | } |
---|
57 | |
---|
58 | static void version (void) |
---|
59 | { |
---|
60 | fprintf(stderr, "%s (written by W. Celes, A. Manzur)\n",TOLUA_VERSION); |
---|
61 | } |
---|
62 | |
---|
63 | static void setfield (lua_State* L, int table, char* f, char* v) |
---|
64 | { |
---|
65 | lua_pushstring(L,f); |
---|
66 | lua_pushstring(L,v); |
---|
67 | lua_settable(L,table); |
---|
68 | } |
---|
69 | |
---|
70 | static void add_extra (lua_State* L, char* value) { |
---|
71 | int len; |
---|
72 | lua_getglobal(L, "_extra_parameters"); |
---|
73 | len = luaL_getn(L, -1); |
---|
74 | lua_pushstring(L, value); |
---|
75 | lua_rawseti(L, -2, len+1); |
---|
76 | lua_pop(L, 1); |
---|
77 | }; |
---|
78 | |
---|
79 | static void error (char* o) |
---|
80 | { |
---|
81 | fprintf(stderr,"tolua: unknown option '%s'\n",o); |
---|
82 | help(); |
---|
83 | exit(1); |
---|
84 | } |
---|
85 | |
---|
86 | int main (int argc, char* argv[]) |
---|
87 | { |
---|
88 | #ifdef LUA_VERSION_NUM /* lua 5.1 */ |
---|
89 | lua_State* L = luaL_newstate(); |
---|
90 | luaL_openlibs(L); |
---|
91 | #else |
---|
92 | lua_State* L = lua_open(); |
---|
93 | luaopen_base(L); |
---|
94 | luaopen_io(L); |
---|
95 | luaopen_string(L); |
---|
96 | luaopen_table(L); |
---|
97 | luaopen_math(L); |
---|
98 | luaopen_debug(L); |
---|
99 | #endif |
---|
100 | |
---|
101 | lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION"); |
---|
102 | lua_pushstring(L,LUA_VERSION); lua_setglobal(L,"TOLUA_LUA_VERSION"); |
---|
103 | |
---|
104 | char* working_directory = ""; |
---|
105 | char* lua_source = ""; |
---|
106 | |
---|
107 | if (argc==1) |
---|
108 | { |
---|
109 | help(); |
---|
110 | return 0; |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | int i, t; |
---|
115 | lua_newtable(L); |
---|
116 | lua_setglobal(L, "_extra_parameters"); |
---|
117 | lua_newtable(L); |
---|
118 | lua_pushvalue(L,-1); |
---|
119 | lua_setglobal(L,"flags"); |
---|
120 | t = lua_gettop(L); |
---|
121 | for (i=1; i<argc; ++i) |
---|
122 | { |
---|
123 | if (*argv[i] == '-') |
---|
124 | { |
---|
125 | switch (argv[i][1]) |
---|
126 | { |
---|
127 | case 'v': version(); return 0; |
---|
128 | case 'h': help(); return 0; |
---|
129 | case 'p': setfield(L,t,"p",""); break; |
---|
130 | case 'P': setfield(L,t,"P",""); break; |
---|
131 | case 'o': setfield(L,t,"o",argv[++i]); break; |
---|
132 | case 'n': setfield(L,t,"n",argv[++i]); break; |
---|
133 | case 'H': setfield(L,t,"H",argv[++i]); break; |
---|
134 | case 'w': |
---|
135 | working_directory = argv[++i]; |
---|
136 | setfield(L,t,"w",argv[i]); |
---|
137 | break; |
---|
138 | case 's': |
---|
139 | lua_source = argv[++i]; |
---|
140 | setfield(L,t,"s",argv[i]); |
---|
141 | break; |
---|
142 | case 'S': setfield(L,t,"S",""); break; |
---|
143 | case '1': setfield(L,t,"1",""); break; |
---|
144 | case 'L': setfield(L,t,"L",argv[++i]); break; |
---|
145 | case 'D': setfield(L,t,"D",""); break; |
---|
146 | case 'W': setfield(L,t,"W",""); break; |
---|
147 | case 'C': setfield(L,t,"C",""); break; |
---|
148 | case 'E': add_extra(L,argv[++i]); break; |
---|
149 | case 't': setfield(L,t,"t",""); break; |
---|
150 | default: error(argv[i]); break; |
---|
151 | } |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | setfield(L,t,"f",argv[i]); |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | lua_pop(L,1); |
---|
160 | } |
---|
161 | |
---|
162 | { |
---|
163 | char path[BUFSIZ]; |
---|
164 | char file[BUFSIZ]; |
---|
165 | |
---|
166 | if (lua_source[0] == '/' || lua_source[0] == '\\') |
---|
167 | { |
---|
168 | strcpy(path, lua_source); |
---|
169 | char* p = strrchr(path, '/'); |
---|
170 | if (p == NULL) |
---|
171 | p = strrchr(path, '\\'); |
---|
172 | p = (p == NULL) ? path : p + 1; |
---|
173 | strcpy(file, p); |
---|
174 | *p = '\0'; |
---|
175 | } |
---|
176 | else |
---|
177 | { |
---|
178 | strcpy(path, working_directory); |
---|
179 | strcpy(file, "all.lua"); |
---|
180 | |
---|
181 | if (strlen(path) > 0) |
---|
182 | { |
---|
183 | char last = path[strlen(path) - 1]; |
---|
184 | if (last != '\\' && last != '/') |
---|
185 | strcat(path, "/"); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | lua_pushstring(L, path); |
---|
190 | lua_setglobal(L, "path"); |
---|
191 | strcat(path, file); |
---|
192 | lua_dofile(L, path); |
---|
193 | } |
---|
194 | return 0; |
---|
195 | } |
---|