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 "lua.h" |
---|
18 | #include "lualib.h" |
---|
19 | #include "lauxlib.h" |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | |
---|
26 | static void help (void) |
---|
27 | { |
---|
28 | fprintf(stderr,"\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 | " -w folder : set working directory; efault is location of package file.\n" |
---|
37 | " -p : parse only.\n" |
---|
38 | " -P : parse and print structure information (for debug).\n" |
---|
39 | " -S : disable support for c++ strings.\n" |
---|
40 | " -1 : substract 1 to operator[] index (for compatibility with tolua5).\n" |
---|
41 | " -L file : run lua file (with dofile()) before doing anything.\n" |
---|
42 | " -D : disable automatic exporting of destructors for classes that have\n" |
---|
43 | " constructors (for compatibility with tolua5)\n" |
---|
44 | " -W : disable warnings for unsupported features (for compatibility\n" |
---|
45 | " with tolua5)\n" |
---|
46 | " -C : disable cleanup of included lua code (for easier debugging)\n" |
---|
47 | " -E value[=value] : add extra values to the luastate\n" |
---|
48 | " -t : export a list of types asociates with the C++ typeid name\n" |
---|
49 | " -h : print this message.\n" |
---|
50 | "Should the input file be omitted, stdin is assumed;\n" |
---|
51 | "in that case, the package name must be explicitly set.\n\n" |
---|
52 | ); |
---|
53 | } |
---|
54 | |
---|
55 | static void version (void) |
---|
56 | { |
---|
57 | fprintf(stderr, "%s (written by W. Celes, A. Manzur)\n",TOLUA_VERSION); |
---|
58 | } |
---|
59 | |
---|
60 | static void setfield (lua_State* L, int table, char* f, char* v) |
---|
61 | { |
---|
62 | lua_pushstring(L,f); |
---|
63 | lua_pushstring(L,v); |
---|
64 | lua_settable(L,table); |
---|
65 | } |
---|
66 | |
---|
67 | static void add_extra (lua_State* L, char* value) { |
---|
68 | int len; |
---|
69 | lua_getglobal(L, "_extra_parameters"); |
---|
70 | len = luaL_getn(L, -1); |
---|
71 | lua_pushstring(L, value); |
---|
72 | lua_rawseti(L, -2, len+1); |
---|
73 | lua_pop(L, 1); |
---|
74 | }; |
---|
75 | |
---|
76 | static void error (char* o) |
---|
77 | { |
---|
78 | fprintf(stderr,"tolua: unknown option '%s'\n",o); |
---|
79 | help(); |
---|
80 | exit(1); |
---|
81 | } |
---|
82 | |
---|
83 | int main (int argc, char* argv[]) |
---|
84 | { |
---|
85 | #ifdef LUA_VERSION_NUM /* lua 5.1 */ |
---|
86 | lua_State* L = luaL_newstate(); |
---|
87 | luaL_openlibs(L); |
---|
88 | #else |
---|
89 | lua_State* L = lua_open(); |
---|
90 | luaopen_base(L); |
---|
91 | luaopen_io(L); |
---|
92 | luaopen_string(L); |
---|
93 | luaopen_table(L); |
---|
94 | luaopen_math(L); |
---|
95 | luaopen_debug(L); |
---|
96 | #endif |
---|
97 | |
---|
98 | lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION"); |
---|
99 | lua_pushstring(L,LUA_VERSION); lua_setglobal(L,"TOLUA_LUA_VERSION"); |
---|
100 | |
---|
101 | if (argc==1) |
---|
102 | { |
---|
103 | help(); |
---|
104 | return 0; |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | int i, t; |
---|
109 | lua_newtable(L); |
---|
110 | lua_setglobal(L, "_extra_parameters"); |
---|
111 | lua_newtable(L); |
---|
112 | lua_pushvalue(L,-1); |
---|
113 | lua_setglobal(L,"flags"); |
---|
114 | t = lua_gettop(L); |
---|
115 | for (i=1; i<argc; ++i) |
---|
116 | { |
---|
117 | if (*argv[i] == '-') |
---|
118 | { |
---|
119 | switch (argv[i][1]) |
---|
120 | { |
---|
121 | case 'v': version(); return 0; |
---|
122 | case 'h': help(); return 0; |
---|
123 | case 'p': setfield(L,t,"p",""); break; |
---|
124 | case 'P': setfield(L,t,"P",""); break; |
---|
125 | case 'o': setfield(L,t,"o",argv[++i]); break; |
---|
126 | case 'n': setfield(L,t,"n",argv[++i]); break; |
---|
127 | case 'H': setfield(L,t,"H",argv[++i]); break; |
---|
128 | case 'w': setfield(L,t,"w",argv[++i]); break; |
---|
129 | case 'S': setfield(L,t,"S",""); break; |
---|
130 | case '1': setfield(L,t,"1",""); break; |
---|
131 | case 'L': setfield(L,t,"L",argv[++i]); break; |
---|
132 | case 'D': setfield(L,t,"D",""); break; |
---|
133 | case 'W': setfield(L,t,"W",""); break; |
---|
134 | case 'C': setfield(L,t,"C",""); break; |
---|
135 | case 'E': add_extra(L,argv[++i]); break; |
---|
136 | case 't': setfield(L,t,"t",""); break; |
---|
137 | default: error(argv[i]); break; |
---|
138 | } |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | setfield(L,t,"f",argv[i]); |
---|
143 | break; |
---|
144 | } |
---|
145 | } |
---|
146 | lua_pop(L,1); |
---|
147 | } |
---|
148 | /* #define TOLUA_SCRIPT_RUN */ |
---|
149 | #ifndef TOLUA_SCRIPT_RUN |
---|
150 | { |
---|
151 | int tolua_tolua_open (lua_State* L); |
---|
152 | tolua_tolua_open(L); |
---|
153 | } |
---|
154 | #else |
---|
155 | { |
---|
156 | char* p; |
---|
157 | char path[BUFSIZ]; |
---|
158 | strcpy(path,argv[0]); |
---|
159 | p = strrchr(path,'/'); |
---|
160 | if (p==NULL) p = strrchr(path,'\\'); |
---|
161 | p = (p==NULL) ? path : p+1; |
---|
162 | sprintf(p,"%s","../src/bin/lua/"); |
---|
163 | lua_pushstring(L,path); lua_setglobal(L,"path"); |
---|
164 | strcat(path,"all.lua"); |
---|
165 | lua_dofile(L,path); |
---|
166 | } |
---|
167 | #endif |
---|
168 | return 0; |
---|
169 | } |
---|