1 /* tolua
2 ** Support code for Lua bindings.
3 ** Written by Waldemar Celes
4 ** TeCGraf/PUC-Rio
5 ** Aug 2003
6 ** $Id: tolua.c,v 1.4 2009/11/24 16:45:12 fabraham Exp $
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 #ifndef LUA_SOURCE
26 int tolua_tolua_open(lua_State *L);
27 #endif
28 
help(void)29 static void help (void)
30 {
31  fprintf(stderr,"\n"
32          "usage: tolua [options] input_file\n"
33          "\n"
34          "Command line options are:\n"
35          "  -v       : print version information.\n"
36          "  -o  file : set output file; default is stdout.\n"
37          "  -H  file : create include file.\n"
38          "  -n  name : set package name; default is input file root name.\n"
39          "  -p       : parse only.\n"
40          "  -P       : parse and print structure information (for debug).\n"
41          "  -h       : print this message.\n"
42          "Should the input file be omitted, stdin is assumed;\n"
43          "in that case, the package name must be explicitly set.\n\n"
44         );
45 }
46 
version(void)47 static void version (void)
48 {
49  fprintf(stderr, "%s (written by W. Celes)\n",TOLUA_VERSION);
50 }
51 
setfield(lua_State * L,int table,char * f,char * v)52 static void setfield (lua_State* L, int table, char* f, char* v)
53 {
54  lua_pushstring(L,f);
55  lua_pushstring(L,v);
56  lua_settable(L,table);
57 }
58 
error(char * o)59 static void error (char* o)
60 {
61  fprintf(stderr,"tolua: unknown option '%s'\n",o);
62  help();
63  exit(1);
64 }
65 
main(int argc,char * argv[])66 int main (int argc, char* argv[])
67 {
68  lua_State* L = luaL_newstate();
69  luaL_openlibs(L);
70 
71  lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION");
72 
73  if (argc==1)
74  {
75   help();
76   return 0;
77  }
78  else
79  {
80   int i, t;
81   lua_newtable(L);
82   lua_pushvalue(L,-1);
83   lua_setglobal(L,"flags");
84   t = lua_gettop(L);
85   for (i=1; i<argc; ++i)
86   {
87    if (*argv[i] == '-')
88    {
89     switch (argv[i][1])
90     {
91      case 'v': version(); return 0;
92      case 'h': help(); return 0;
93      case 'p': setfield(L,t,"p",""); break;
94      case 'P': setfield(L,t,"P",""); break;
95      case 'o': setfield(L,t,"o",argv[++i]); break;
96      case 'n': setfield(L,t,"n",argv[++i]); break;
97      case 'H': setfield(L,t,"H",argv[++i]); break;
98      default: error(argv[i]); break;
99     }
100    }
101    else
102    {
103     setfield(L,t,"f",argv[i]);
104     break;
105    }
106   }
107   lua_pop(L,1);
108  }
109 
110 #ifndef LUA_SOURCE
111  {
112   tolua_tolua_open(L);
113  }
114 #else
115  {
116   char* p;
117   char  path[BUFSIZ] = "";
118   strcpy(path,argv[0]);
119   p = strrchr(path,'/');
120   if (p==NULL) p = strrchr(path,'\\');
121   p = (p==NULL) ? path : p+1;
122   sprintf(p,"%s","../src/bin/lua/");
123   lua_pushstring(L,path); lua_setglobal(L,"path");
124   strcat(path,"all.lua");
125   if (luaL_dofile(L,path)) {
126    printf("error loading all.lua: %s\n",lua_tostring(L,-1));
127    return 1;
128   }
129  }
130 #endif
131  return 0;
132 }
133