Debian Codegraph @

3rdparty/toluapp/src/bin/tolua.c

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