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