[1129] | 1 | /* tolua |
---|
| 2 | ** Support code for Lua bindings. |
---|
| 3 | ** Written by Waldemar Celes |
---|
| 4 | ** TeCGraf/PUC-Rio |
---|
| 5 | ** Aug 2003 |
---|
| 6 | ** $Id:$ |
---|
[2509] | 7 | ** Extension by Orxonox (Reto Grieder) to support working directory |
---|
| 8 | ** and direct usage of lua files. (2008) |
---|
[1129] | 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 | |
---|
[2509] | 19 | #include "lua.h" |
---|
| 20 | #include "lualib.h" |
---|
| 21 | #include "lauxlib.h" |
---|
[1129] | 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" |
---|
[2509] | 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" |
---|
[1129] | 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) { |
---|
[2509] | 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); |
---|
[1129] | 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 | { |
---|
[2509] | 88 | char* working_directory = ""; |
---|
| 89 | char* lua_source = ""; |
---|
| 90 | |
---|
[1129] | 91 | #ifdef LUA_VERSION_NUM /* lua 5.1 */ |
---|
| 92 | lua_State* L = luaL_newstate(); |
---|
| 93 | luaL_openlibs(L); |
---|
| 94 | #else |
---|
| 95 | lua_State* L = lua_open(); |
---|
| 96 | luaopen_base(L); |
---|
| 97 | luaopen_io(L); |
---|
| 98 | luaopen_string(L); |
---|
| 99 | luaopen_table(L); |
---|
| 100 | luaopen_math(L); |
---|
| 101 | luaopen_debug(L); |
---|
| 102 | #endif |
---|
| 103 | |
---|
| 104 | lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION"); |
---|
| 105 | lua_pushstring(L,LUA_VERSION); lua_setglobal(L,"TOLUA_LUA_VERSION"); |
---|
| 106 | |
---|
[2509] | 107 | |
---|
[1129] | 108 | if (argc==1) |
---|
| 109 | { |
---|
| 110 | help(); |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | int i, t; |
---|
| 116 | lua_newtable(L); |
---|
| 117 | lua_setglobal(L, "_extra_parameters"); |
---|
| 118 | lua_newtable(L); |
---|
| 119 | lua_pushvalue(L,-1); |
---|
| 120 | lua_setglobal(L,"flags"); |
---|
| 121 | t = lua_gettop(L); |
---|
| 122 | for (i=1; i<argc; ++i) |
---|
| 123 | { |
---|
| 124 | if (*argv[i] == '-') |
---|
| 125 | { |
---|
| 126 | switch (argv[i][1]) |
---|
| 127 | { |
---|
| 128 | case 'v': version(); return 0; |
---|
| 129 | case 'h': help(); return 0; |
---|
| 130 | case 'p': setfield(L,t,"p",""); break; |
---|
| 131 | case 'P': setfield(L,t,"P",""); break; |
---|
| 132 | case 'o': setfield(L,t,"o",argv[++i]); break; |
---|
| 133 | case 'n': setfield(L,t,"n",argv[++i]); break; |
---|
| 134 | case 'H': setfield(L,t,"H",argv[++i]); break; |
---|
[2509] | 135 | case 'w': |
---|
| 136 | working_directory = argv[++i]; |
---|
| 137 | setfield(L,t,"w",argv[i]); |
---|
| 138 | break; |
---|
| 139 | case 's': |
---|
| 140 | lua_source = argv[++i]; |
---|
| 141 | setfield(L,t,"s",argv[i]); |
---|
| 142 | break; |
---|
[1129] | 143 | case 'S': setfield(L,t,"S",""); break; |
---|
| 144 | case '1': setfield(L,t,"1",""); break; |
---|
| 145 | case 'L': setfield(L,t,"L",argv[++i]); break; |
---|
| 146 | case 'D': setfield(L,t,"D",""); break; |
---|
| 147 | case 'W': setfield(L,t,"W",""); break; |
---|
| 148 | case 'C': setfield(L,t,"C",""); break; |
---|
| 149 | case 'E': add_extra(L,argv[++i]); break; |
---|
| 150 | case 't': setfield(L,t,"t",""); break; |
---|
| 151 | default: error(argv[i]); break; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | setfield(L,t,"f",argv[i]); |
---|
| 157 | break; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | lua_pop(L,1); |
---|
| 161 | } |
---|
[2509] | 162 | |
---|
[1129] | 163 | { |
---|
[2509] | 164 | char path[BUFSIZ]; |
---|
| 165 | char file[BUFSIZ]; |
---|
| 166 | path[0] = '\0'; |
---|
| 167 | file[0] = '\0'; |
---|
| 168 | |
---|
| 169 | if (strlen(lua_source) > 0 && |
---|
| 170 | lua_source[0] != '/' && |
---|
| 171 | lua_source[0] != '\\' && |
---|
| 172 | strlen(lua_source) > 1 && |
---|
| 173 | lua_source[1] != ':') |
---|
| 174 | { |
---|
| 175 | /* Relative path, prefix working directory */ |
---|
| 176 | strcpy(path, working_directory); |
---|
| 177 | /* Make sure there is '\\' or '/' at the end of the path */ |
---|
| 178 | if (strlen(path) > 0) |
---|
| 179 | { |
---|
| 180 | char last = path[strlen(path) - 1]; |
---|
| 181 | if (last != '\\' && last != '/') |
---|
| 182 | strcat(path, "/"); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | strcat(path, lua_source); |
---|
| 187 | |
---|
| 188 | /* Extract the full path */ |
---|
| 189 | { |
---|
| 190 | char* p; |
---|
| 191 | p = strrchr(path, '/'); |
---|
| 192 | if (p == NULL) |
---|
| 193 | p = strrchr(path, '\\'); |
---|
| 194 | p = (p == NULL) ? path : p + 1; |
---|
| 195 | strcpy(file, p); |
---|
| 196 | *p = '\0'; |
---|
| 197 | } |
---|
| 198 | if (strlen(file) == 0) |
---|
| 199 | strcpy(file, "all.lua"); |
---|
| 200 | |
---|
| 201 | lua_pushstring(L, path); |
---|
| 202 | lua_setglobal(L, "path"); |
---|
| 203 | strcat(path, file); |
---|
| 204 | #ifdef LUA_VERSION_NUM /* lua 5.1 */ |
---|
| 205 | luaL_dofile(L, path); |
---|
[1129] | 206 | #else |
---|
[2509] | 207 | lua_dofile(L, path); |
---|
| 208 | #endif |
---|
[1129] | 209 | } |
---|
| 210 | return 0; |
---|
| 211 | } |
---|