Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/tolua/tolua.c @ 3127

Last change on this file since 3127 was 3127, checked in by rgrieder, 15 years ago

Update to tolua 1.0.93

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
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
28static 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         "  -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
59static void version (void)
60{
61 fprintf(stderr, "%s (written by W. Celes, A. Manzur)\n",TOLUA_VERSION);
62}
63
64static void setfield (lua_State* L, int table, char* f, char* v)
65{
66 lua_pushstring(L,f);
67 lua_pushstring(L,v);
68 lua_settable(L,table);
69}
70
71static void add_extra (lua_State* L, char* value) {
72 int len;
73 lua_getglobal(L, "_extra_parameters");
74 len = luaL_getn(L, -1);
75 lua_pushstring(L, value);
76 lua_rawseti(L, -2, len+1);
77 lua_pop(L, 1);
78};
79
80static void error (char* o)
81{
82 fprintf(stderr,"tolua: unknown option '%s'\n",o);
83 help();
84 exit(1);
85}
86
87int main (int argc, char* argv[])
88{
89 char* working_directory = "";
90 char* lua_source = "";
91
92 #ifdef LUA_VERSION_NUM /* lua 5.1 */
93 lua_State* L = luaL_newstate();
94 luaL_openlibs(L);
95 #else
96 lua_State* L = lua_open();
97 luaopen_base(L);
98 luaopen_io(L);
99 luaopen_string(L);
100 luaopen_table(L);
101 luaopen_math(L);
102 luaopen_debug(L);
103 #endif
104
105 lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION");
106 lua_pushstring(L,LUA_VERSION); lua_setglobal(L,"TOLUA_LUA_VERSION");
107
108
109 if (argc==1)
110 {
111  help();
112  return 0;
113 }
114 else
115 {
116  int i, t;
117  lua_newtable(L);
118  lua_setglobal(L, "_extra_parameters");
119  lua_newtable(L);
120  lua_pushvalue(L,-1);
121  lua_setglobal(L,"flags");
122  t = lua_gettop(L);
123  for (i=1; i<argc; ++i)
124  {
125   if (*argv[i] == '-')
126   {
127    switch (argv[i][1])
128    {
129     case 'v': version(); return 0;
130     case 'h': help(); return 0;
131     case 'p': setfield(L,t,"p",""); break;
132     case 'P': setfield(L,t,"P",""); break;
133     case 'o': setfield(L,t,"o",argv[++i]); break;
134     case 'n': setfield(L,t,"n",argv[++i]); break;
135     case 'H': setfield(L,t,"H",argv[++i]); break;
136     case 'w':
137      working_directory = argv[++i];
138      setfield(L,t,"w",argv[i]);
139      break;
140     case 's':
141      lua_source = argv[++i];
142      setfield(L,t,"s",argv[i]);
143      break;
144     case 'S': setfield(L,t,"S",""); break;
145     case '1': setfield(L,t,"1",""); break;
146     case 'L': setfield(L,t,"L",argv[++i]); break;
147     case 'D': setfield(L,t,"D",""); break;
148     case 'W': setfield(L,t,"W",""); break;
149     case 'C': setfield(L,t,"C",""); break;
150     case 'E': add_extra(L,argv[++i]); break;
151     case 't': setfield(L,t,"t",""); break;
152     case 'q': setfield(L,t,"q",""); break;
153     default: error(argv[i]); break;
154    }
155   }
156   else
157   {
158    setfield(L,t,"f",argv[i]);
159    break;
160   }
161  }
162  lua_pop(L,1);
163 }
164
165 {
166  char path[BUFSIZ];
167  char file[BUFSIZ];
168  path[0] = '\0';
169  file[0] = '\0';
170
171  if (strlen(lua_source) > 0 &&
172      lua_source[0] != '/' &&
173      lua_source[0] != '\\' &&
174      strlen(lua_source) > 1 &&
175      lua_source[1] != ':')
176  {
177   /* Relative path, prefix working directory */
178   strcpy(path, working_directory);
179   /* Make sure there is '\\' or '/' at the end of the path */
180   if (strlen(path) > 0)
181   {
182    char last = path[strlen(path) - 1];
183    if (last != '\\' && last != '/')
184     strcat(path, "/");
185   }
186  }
187
188  strcat(path, lua_source);
189
190  /* Extract the full path */
191  {
192   char* p;
193   p = strrchr(path, '/');
194   if (p == NULL)
195    p = strrchr(path, '\\');
196   p = (p == NULL) ? path : p + 1;
197   strcpy(file, p);
198   *p = '\0';
199  }
200  if (strlen(file) == 0)
201   strcpy(file, "all.lua");
202
203  lua_pushstring(L, path);
204  lua_setglobal(L, "path");
205  strcat(path, file);
206#ifdef LUA_VERSION_NUM /* lua 5.1 */
207  luaL_dofile(L, path);
208#else
209  lua_dofile(L, path);
210#endif
211 }
212 return 0;
213}
Note: See TracBrowser for help on using the repository browser.