Beispiel #1
0
int main(int argc, char** argv)
{
    for (int n=1; n<argc; ++n) 
    {
        const char* file=argv[n];

        lua_State *L = lua_open();
        luaopen_io(L); // provides io.*
        luaopen_base(L);
        luaopen_table(L);
        luaopen_math(L);
        // luaopen_loadlib(L);
        // luaopen_loadlib(L);

        std::cerr << "-- Loading file:" << file << std::endl;

        int s = luaL_loadfile(L, file);

        if (s==0) {
            // execute Lua program
            s = lua_pcall(L, 0, LUA_MULTRET, 0);
        }

        report_errors(L, s);
        lua_close(L);
        std::cerr << std::endl;
    }
    return 0;
}
Beispiel #2
0
int ltapi_libraries_load( lua_State * L )
{
    /* load required library and discard any possible results remaining at
     * the stack */

    luaopen_base( L );
    lua_settop( L, 0 );

    luaopen_table( L );
    lua_settop( L, 0 );

    luaopen_io( L );
    lua_settop( L, 0 );

    luaopen_string( L );
    lua_settop( L, 0 );

    luaopen_math( L );
    lua_settop( L, 0 );

    luaopen_debug( L );
    lua_settop( L, 0 );

    luaopen_loadlib( L );
    lua_settop( L, 0 );

    return 0;
}
Beispiel #3
0
void LuaState::Init( bool initStandardLibrary )
{
	// Register some basic functions with Lua.
	if (initStandardLibrary)
	{
		// A "bug" in Lua 5.01 causes stack entries to be left behind.
		LuaAutoBlock autoBlock(this);
		luaopen_base(m_state);
		luaopen_table( m_state );
		luaopen_io(m_state);
		luaopen_string(m_state);
		luaopen_wstring(m_state);
		luaopen_math(m_state);
		luaopen_debug(m_state);
#ifndef _WIN32_WCE
		luaopen_loadlib(m_state);
#endif _WIN32_WCE

		ScriptFunctionsRegister( this );

		GetGlobals().Register("LuaDumpGlobals", LS_LuaDumpGlobals);
		GetGlobals().Register("LuaDumpObject", LS_LuaDumpObject);
		GetGlobals().Register("LuaDumpFile", LS_LuaDumpFile);
	}

	GetGlobals().Register("LOG", LS_LOG);
	GetGlobals().Register("_ALERT", LS_LOG);

	lua_atpanic( m_state, FatalError );
}
Beispiel #4
0
ActionScript::ActionScript(Game *igame,const std::string &datadir,const std::string &scriptname):
game(igame),
_player(NULL)
{
	this->loaded = false;
	lastuid = 0;
	if(scriptname == "")
		return;
	luaState = lua_open();
	luaopen_loadlib(luaState);
	luaopen_base(luaState);
	luaopen_math(luaState);
	luaopen_string(luaState);
	luaopen_io(luaState);
    lua_dofile(luaState, std::string(datadir + "actions/lib/actions.lua").c_str());

#ifdef USING_VISUAL_2005
	FILE* in = NULL;
	fopen_s(&in, scriptname.c_str(), "r");
#else
	FILE* in=fopen(scriptname.c_str(), "r");
#endif //USING_VISUAL_2005
	if(!in){
		std::cout << "Error: Can not open " << scriptname.c_str() << std::endl;
		return;
	}
	else
		fclose(in);
	lua_dofile(luaState, scriptname.c_str());
	this->setGlobalNumber("addressOfActionScript", (int)this);
	this->loaded = true;
	this->registerFunctions();
}
Beispiel #5
0
int main(void) {
	char buff[256];
	char buf[256];
	int error;
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	luaopen_base(L);             /* opens the basic library */
	luaopen_table(L);            /* opens the table library */
	luaopen_io(L);               /* opens the I/O library */
	luaopen_string(L);           /* opens the string lib. */
	luaopen_math(L);             /* opens the math lib. */
	char s1[] = "lzkx";
	char s2 = "zny";
	printf("input: ddd\n");
	sacnf_s();
	while (fgets(buff, sizeof(buff), stdin) != NULL) {
  		sprintf_s(buf,256, "print ( %s)", buff,256);
		error = luaL_loadbuffer(L, buf, strlen(buf), "line") ||
			lua_pcall(L, 0, 0, 0);
		if (error) {
			fprintf(stderr, "%s", lua_tostring(L, -1));
			lua_pop(L, 1);  /* pop error message from the stack */
			
		}

	}

	lua_close(L);
	return 0;

}
Beispiel #6
0
//加载lua
void load(char *filename, int *width, int *height) 
{
	lua_State *L = luaL_newstate();
	luaopen_base(L);
	luaopen_io(L);
	luaopen_string(L);
	luaopen_math(L);
	if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))
	{
		error(L, "cannot run configuration file: %s", lua_tostring(L, -1));
	}
		
	lua_getglobal(L, "width");
	lua_getglobal(L, "height");
	if (!lua_isnumber(L, -2))	
	{
		error(L, "'width' should be a number\n");
	}
	if (!lua_isnumber(L, -1))
	{
		error(L, "'height' should be a number\n");
	}
	*width = (int)lua_tonumber(L, -2);
	*height = (int)lua_tonumber(L, -1);
	lua_close(L);
}
Beispiel #7
0
int
main(int argc, char *argv[])
{
	if (argc != 2) {
		usage(stderr, argv[0]);
		exit(EXIT_FAILURE);
	}

	if (access(argv[1], R_OK)) {
		fprintf(stderr, "File %s is unavailable.\n", argv[1]);
		exit(EXIT_FAILURE);
	}
	
        L = lua_open();
        lua_baselibopen(L);
	luaopen_math(L);
	luaopen_io(L);
	luaopen_string(L);

	register_ldns_functions();

        /* run the script */
        lua_dofile(L, argv[1]);

        lua_close(L);
        exit(EXIT_SUCCESS);
}
Beispiel #8
0
bool               P3DPlugLuaRunScript(const char         *FileName,
                                       const P3DPlantModel*PlantModel)
 {
  lua_State                           *State;

  SetCLocale();

  State = lua_open();

  if (State != NULL)
   {
    #if NGP_LUA_VER > 50
    luaL_openlibs(State);
    #else
    luaopen_base(State);
    luaopen_table(State);
    luaopen_io(State);
    luaopen_string(State);
    luaopen_math(State);
    #endif

    P3DPlugLuaRegisterHLI(State);
    P3DPlugLuaRegisterUI(State);
    P3DPlugLuaRegisterFS(State);
    P3DPlugLuaRegisterExportPrefs(State,P3DApp::GetApp()->GetExport3DPrefs());
    P3DPlugLuaRegisterModel(State,"PlantModel",PlantModel);

    lua_register(State,"GetTextureFileName",GetTextureFileName);
    lua_register(State,"GetCurrentLOD",GetCurrentLOD);
    lua_register(State,"GetDerivedFileName",GetDerivedFileName);

    if (luaL_loadfile(State,FileName) || lua_pcall(State,0,0,0))
     {
      if (lua_isstring(State,-1))
       {
        DisplayErrorMessage(wxString(lua_tostring(State,-1),wxConvUTF8));
       }
      else
       {
        DisplayErrorMessage(wxT("Script error: (undefined)"));
       }
     }

    lua_close(State);

    RestoreLocale();

    return(true);
   }
  else
   {
    DisplayErrorMessage(wxT("Unable to initialize Lua environment"));

    RestoreLocale();

    return(false);
   }
 }
Beispiel #9
0
 int open_io()
 {
     if (L)
     {
         luaopen_io(L);
         return 0;
     }
     return -1;
 }
Beispiel #10
0
void scripting_Init() {
  L = lua_open();
  luaopen_base(L);
  luaopen_table(L);
  luaopen_string(L);
  luaopen_io(L);

  // init_c_interface(L);
}
Beispiel #11
0
static void open_libs(lua_State *L) 
{
    luaopen_io(L);
    luaopen_base(L);
    luaopen_table(L);
    luaopen_string(L);
    luaopen_math(L);
    // luaopen_loadlib(L);
}
Beispiel #12
0
/*************************************************************************
	Constructor (creates Lua state)
*************************************************************************/
LuaScriptModule::LuaScriptModule(lua_State* state) :
    d_ownsState(state == 0),
    d_state(state),
    d_errFuncIndex(LUA_NOREF),
    d_activeErrFuncIndex(LUA_NOREF)
{
    // initialise and create a lua_State if one was not provided
    if (!d_state)
    {
        #if CEGUI_LUA_VER >= 51
            static const luaL_Reg lualibs[] = {
                {"", luaopen_base},
                {LUA_LOADLIBNAME, luaopen_package},
                {LUA_TABLIBNAME, luaopen_table},
                {LUA_IOLIBNAME, luaopen_io},
                {LUA_OSLIBNAME, luaopen_os},
                {LUA_STRLIBNAME, luaopen_string},
                {LUA_MATHLIBNAME, luaopen_math},
            #if defined(DEBUG) || defined (_DEBUG)
                    {LUA_DBLIBNAME, luaopen_debug},
            #endif
                {0, 0}
            };
        #endif /* CEGUI_LUA_VER >= 51 */

        // create a lua state
        d_ownsState = true;
#if LUA_VERSION_NUM > 501
        d_state = luaL_newstate();
#else
        d_state = lua_open();
#endif

        // init all standard libraries
        #if CEGUI_LUA_VER >= 51
                const luaL_Reg *lib = lualibs;
                for (; lib->func; lib++)
                {
                    lua_pushcfunction(d_state, lib->func);
                    lua_pushstring(d_state, lib->name);
                    lua_call(d_state, 1, 0);
                }
        #else /* CEGUI_LUA_VER >= 51 */
            luaopen_base(d_state);
            luaopen_io(d_state);
            luaopen_string(d_state);
            luaopen_table(d_state);
            luaopen_math(d_state);
            #if defined(DEBUG) || defined (_DEBUG)
                luaopen_debug(d_state);
            #endif
        #endif /* CEGUI_LUA_VER >= 51 */
    }

    setModuleIdentifierString();
}
Beispiel #13
0
void LuaPhysicsSetup::initPhysics()
{
	m_guiHelper->setUpAxis(upaxis);
	const char* prefix[]={"./","./data/","../data/","../../data/","../../../data/","../../../../data/"};
	int numPrefixes = sizeof(prefix)/sizeof(const char*);
	char relativeFileName[1024];
	FILE* f=0;
	int result = 0;

	for (int i=0;!f && i<numPrefixes;i++)
	{
		sprintf(relativeFileName,"%s%s",prefix[i],sLuaFileName);
		f = fopen(relativeFileName,"rb");
	}
	if (f)
	{
		fclose(f);

		lua_State *L = luaL_newstate();

		luaopen_io(L); // provides io.*
		luaopen_base(L);
		luaopen_table(L);
		luaopen_string(L);
		luaopen_math(L);
		//luaopen_package(L);
		luaL_openlibs(L);

		 // make my_function() available to Lua programs
		lua_register(L, "createDefaultDynamicsWorld", gCreateDefaultDynamicsWorld);
		lua_register(L, "deleteDynamicsWorld", gDeleteDynamicsWorld);
		lua_register(L, "createCubeShape", gCreateCubeShape);
		lua_register(L, "createSphereShape", gCreateSphereShape);
		lua_register(L, "loadMultiBodyFromUrdf",gLoadMultiBodyFromUrdf);

		lua_register(L, "createRigidBody", gCreateRigidBody);
		lua_register(L, "setBodyPosition", gSetBodyPosition);
		lua_register(L, "setBodyOrientation", gSetBodyOrientation);



		int s = luaL_loadfile(L, relativeFileName);

		if ( s==0 ) {
		  // execute Lua program
		  s = lua_pcall(L, 0, LUA_MULTRET, 0);
		}

		report_errors(L, s);
		lua_close(L);
	} else
	{
		b3Error("Cannot find Lua file%s\n",sLuaFileName);
	}

}
Beispiel #14
0
static void 
open_std_libs(lua_State *l) 
{
    luaopen_base(l);   lua_settop(l, 0); 
    luaopen_table(l);  lua_settop(l, 0); 
    luaopen_io(l);     lua_settop(l, 0); 
    luaopen_string(l); lua_settop(l, 0); 
    luaopen_debug(l);  lua_settop(l, 0); 
    luaopen_math(l);   lua_settop(l, 0); 
}
Beispiel #15
0
extern
P3DGMeshData      *P3DPlugLuaRunGMeshGenerator
                                      (const char         *FileName)
 {
  lua_State                           *State;
  P3DGMeshData                        *Result = 0;

  SetCLocale();

  State = lua_open();

  if (State != NULL)
   {
    #if NGP_LUA_VER > 50
    luaL_openlibs(State);
    #else
    luaopen_base(State);
    luaopen_table(State);
    luaopen_io(State);
    luaopen_string(State);
    luaopen_math(State);
    #endif

    P3DPlugLuaRegisterUI(State);

    if (luaL_loadfile(State,FileName) || lua_pcall(State,0,1,0))
     {
      if (lua_isstring(State,-1))
       {
        DisplayErrorMessage(wxString(lua_tostring(State,-1),wxConvUTF8));
       }
      else
       {
        DisplayErrorMessage(wxT("Script error: (undefined)"));
       }
     }
    else
     {
      Result = CreateGMeshDataFromLuaTable(State);
     }

    lua_close(State);

    RestoreLocale();
   }
  else
   {
    DisplayErrorMessage(wxT("Unable to initialize Lua environment"));

    RestoreLocale();
   }

  return(Result);
 }
Beispiel #16
0
	void lua_context::init()
	{
		// initialize Lua
		state_.reset(luaL_newstate(), [](lua_State* L) { lua_close(L); });

		// load various Lua libraries
		luaopen_base(context_ptr());
		luaopen_string(context_ptr());
		luaopen_table(context_ptr());
		luaopen_math(context_ptr());
		luaopen_io(context_ptr());
		luaopen_debug(context_ptr());
		luaL_openlibs(context_ptr());

		luaL_newmetatable(context_ptr(), function_str);
		luaL_setfuncs(context_ptr(), gFFLFunctions, 0);	

		luaL_newmetatable(context_ptr(), callable_str);
		luaL_setfuncs(context_ptr(), gCallableFunctions, 0);

		luaL_newmetatable(context_ptr(), callable_function_str);
		luaL_setfuncs(context_ptr(), gFFLCallableFunctions, 0);

		luaL_newmetatable(context_ptr(), lib_functions_str);
		luaL_setfuncs(context_ptr(), gLibMetaFunctions, 0);

		push_anura_table(context_ptr());

		/*dostring(
			"local lvl = Anura.level()\n"
			"print(lvl.id, lvl.music_volume, lvl.in_editor)\n"
			"Anura.debug('abcd')\n"
			"Anura.eval('debug(map(range(10),value))')\n"
			"local camera = lvl.camera\n"
			"print('Camera speed: ' .. camera.speed)\n"
			"for n = 1, 10 do\n"
			"  camera.speed = n\n"
			"  print('Camera speed: ' .. camera.speed)\n"
			"end\n"
			"lvl.player:debug_rect(lvl.player.mid_x-50, lvl.player.mid_y-50, 100, 100)\n"
			//"for i,v in ipairs(lvl.active_chars) do\n"
			//"for i,v in ipairs(Anura.eval('range(10)')) do\n"
			"local mm = Anura.eval('{q(a):1,q(b):2}')\n"
			//"print(mm.a)\n"
			"for i,v in pairs(mm) do\n"
			"  print(i, v)\n"
			"end\n"
		);*/

		/*dostring("local me = Anura.me\n"
			"print(me.speed)",
			level::current().camera().get()
		);*/
	}
Beispiel #17
0
cLua::cLua()
{
	m_pErrorHandler = NULL;

	m_pScriptContext = lua_open();
	luaopen_base(m_pScriptContext);
	luaopen_io(m_pScriptContext);
	luaopen_string(m_pScriptContext);
	luaopen_math(m_pScriptContext);
	luaopen_debug(m_pScriptContext);
	luaopen_table(m_pScriptContext);
}
Beispiel #18
0
int open_iolibext (lua_State *L) {
#ifdef LuajitTeX
  return luaopen_io(L);
#else
  luaL_newlib(L, iolib);  /* new module */
  createmeta(L);
  /* create (and set) default files */
  createstdfile(L, stdin, IO_INPUT, "stdin");
  createstdfile(L, stdout, IO_OUTPUT, "stdout");
  createstdfile(L, stderr, NULL, "stderr");
  return 1;
#endif
}
Beispiel #19
0
lua_State* LuaWorld::getState()
{
    if(GlobalState==NULL)
    {
        GlobalState = luaL_newstate();
        luaopen_io(GlobalState);
        luaopen_base(GlobalState);
        luaopen_table(GlobalState);
        luaopen_string(GlobalState);
        luaopen_math(GlobalState);
        luaL_openlibs(GlobalState);
    }
    return GlobalState;
}
Beispiel #20
0
initlua(void)
{
	PyObject *m;
	m = Py_InitModule("lua", lua_methods);

	if (!L) {
		L = lua_open();
		luaopen_base(L);
		luaopen_table(L);
		luaopen_io(L);
		luaopen_string(L);
		luaopen_debug(L);
		luaopen_loadlib(L);
		luaopen_python(L);
		lua_settop(L, 0);
	}
}
Beispiel #21
0
lua_State *lxc_new_state() 
{
#if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM >= 501)
	lua_State *L = luaL_newstate();     /* opens Lua */
	luaL_openlibs(L);
#else
	lua_State *L = lua_open();     /* opens Lua */
	luaopen_base(L);    /* opens the basic library */
	luaopen_table(L);   /* opens the table library */
	luaopen_io(L);      /* opens the I/O library */
	luaopen_string(L);  /* opens the string lib. */
	luaopen_math(L);    /* opens the math lib. */
#endif

	luaopen_xchat(L);
	return L;
}
Beispiel #22
0
int main(int argc, char *argv[])
{
    /* initialize Lua */
    L = lua_open();

    /* load various Lua libraries */
    lua_baselibopen(L);
    luaopen_table(L);
    luaopen_io(L);
    luaopen_string(L);
    luaopen_math(L);

    /* cleanup Lua */
    lua_close(L);

    return 0;
}
Beispiel #23
0
bool 
runLua(const std::string& filename)
{
   GRLUA_DEBUG("Importing scene from " << filename);
   
   // Start a lua interpreter
   lua_State* L = lua_open();
   
   GRLUA_DEBUG("Loading base libraries");
   
   // Load some base library
   luaopen_base(L);
   luaopen_io(L);
   luaopen_string(L);
   luaopen_math(L);
   luaopen_table(L);
   
   GRLUA_DEBUG("Setting up our functions");
   
   // Set up the metatable for gr.node
   luaL_newmetatable(L, "gr.node");
   lua_pushstring(L, "__index");
   lua_pushvalue(L, -2);
   lua_settable(L, -3);
   
   // Load the gr.node methods
   luaL_openlib(L, 0, grlib_node_methods, 0);
   
   // Load the gr functions
   luaL_openlib(L, "gr", grlib_functions, 0);
   
   GRLUA_DEBUG("Parsing the scene");
   
   // Now parse the actual scene
   if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) {
      std::cerr << "Error loading " << filename << ": " << lua_tostring(L, -1) << std::endl;
      return false;
   }
   GRLUA_DEBUG("Closing the interpreter");
   
   // Close the interpreter, free up any resources not needed
   lua_close(L);
   
   return true;
}
Beispiel #24
0
void ScriptManager::openLuaState() {
	_luaState = lua_open();
	if (!_luaState) {
		throw Common::Exception("Failed to open Lua state");
	}

	luaopen_base(_luaState);
	luaopen_io(_luaState);
	luaopen_math(_luaState);
	luaopen_string(_luaState);
	luaopen_table(_luaState);
	luaopen_loadlib(_luaState);
	luaopen_debug(_luaState);

	tolua_open(_luaState);

	lua_atpanic(_luaState, &ScriptManager::atPanic);
}
static lua_State *filtro_abrir_lua(modulo_t *modulo, const char *ruta) {
  dato_filtro_t * dato = (dato_filtro_t*)modulo->m_dato;
  lua_State *l;
  static const struct luaL_reg arraylib [] = {
    {"set", filtro_gestos_setarray},
    {"get", filtro_gestos_getarray},
    {"size", filtro_gestos_getsize},
    {"copiar", filtro_gestos_copiar},
    {"crear_copia", filtro_gestos_crear_copia},
    {"difuminar", filtro_gestos_difuminar},
    {"centrar", filtro_gestos_centrar},
    {"rotar", filtro_gestos_rotar},
    {"centrar2", filtro_gestos_centrar2},
    {"buscar_limites", filtro_gestos_buscar_limites},
    {"clean", filtro_gestos_clean},
    {"make_up", filtro_gestos_make_up},
    {"on_border", filtro_gestos_on_border},
    {"borrar_bounds", filtro_gestos_borrar_bounds},
    {NULL, NULL}
  };

  static const struct luaL_reg arrayparam [] = {
    {"get_colores", filtro_gestos_get_colores},
    {NULL, NULL}
  };

  dato->m_lua = lua_open();
  l = dato->m_lua;
  luaL_openlib(l, "imagen", arraylib, 0);
  luaL_openlib(l, "parametros", arrayparam, 0);

  luaopen_base(l);
  luaopen_table(l);
  luaopen_io(l);
  luaopen_string(l);
  luaopen_math(l);
  luaopen_loadlib(l);    

  luaL_loadfile(l, ruta);
  lua_pcall(l, 0, 0, 0);

  return l;
}
Beispiel #26
0
CLuaBridge::CLuaBridge(void) : m_pLuaContext(NULL)
{
	m_pLuaContext = lua_open();

	if (m_pLuaContext != NULL)
	{
		lua_pushcclosure(m_pLuaContext, LuaDebugMessage, 0);
		lua_setglobal(m_pLuaContext, "trace");

		lua_atpanic(m_pLuaContext, (lua_CFunction)HandleLuaAsssert);

		luaopen_io(m_pLuaContext);
		luaopen_math(m_pLuaContext);
		luaopen_base(m_pLuaContext);
		luaopen_loadlib(m_pLuaContext);
		luaopen_table(m_pLuaContext);
		luaopen_string(m_pLuaContext);
		luaopen_debug(m_pLuaContext);		
	}
}
Beispiel #27
0
int main(int argc, char **argv)
{
  pthread_t threads[NUM_THREADS];
  pthread_mutex_init(&mutex_lua, NULL);
  lua_State *L = lua_open();
  
  // load the libs
#ifdef LUA51
  // lua 5.1
  luaL_openlibs(L);
#else
  // lua 5.0
  luaopen_base(L);             /* opens the basic library */
  luaopen_table(L);            /* opens the table library */
  luaopen_io(L);               /* opens the I/O library */
  luaopen_string(L);           /* opens the string lib. */
  luaopen_math(L);             /* opens the math lib. */
#endif

  // run a Lua script
  if (luaL_loadfile(L, LUA_SCRIPT) || lua_pcall(L, 0, 0, 0))
    error(L, "cannot run the lua script: %s",
             lua_tostring(L, -1));

  unsigned int t;
  for (t=0; t<NUM_THREADS; t++) {
    printf("In main: creating thread %d\n", t);
    thread_context_s *tx;
    tx->L   = L;
    tx->tid = t;
    (void)pthread_create(&threads[t], NULL, thread_context, (void *)tx);
  }
  for (t=0;t<NUM_THREADS;t++) {
    (void)pthread_join(threads[t], NULL);
    printf("joined thread %d\n", t);
  }
  pthread_exit(NULL);
  lua_close(L);
  return 0;
}
Beispiel #28
0
int main()
{
    //create a lua state
    lua_State* pL = lua_open();

    //enable access to the standard libraries
    luaopen_base(pL);
    luaopen_string(pL);
    luaopen_table(pL);
    luaopen_math(pL);
    luaopen_io(pL);

    if (int error = lua_dofile(pL, "your_first_lua_script.lua") != 0)
    {
        std::cout << "\n[C++]: ERROR(" << error << "): Problem with lua script file!\n\n" << std::endl;

        return 0;
    }

    //tidy up
    lua_close(pL);

    return 0;
}
Beispiel #29
0
int
weechat_lua_load (const char *filename)
{
    FILE *fp;
    char *weechat_lua_code = {
        "weechat_outputs = {\n"
        "    write = function (self, str)\n"
        "        weechat.print(\"\", \"lua: stdout/stderr: \" .. str)\n"
        "    end\n"
        "}\n"
        "io.stdout = weechat_outputs\n"
        "io.stderr = weechat_outputs\n"
    };

    if ((fp = fopen (filename, "r")) == NULL)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: script \"%s\" not found"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        return 0;
    }

    if ((weechat_lua_plugin->debug >= 2) || !lua_quiet)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s: loading script \"%s\""),
                        LUA_PLUGIN_NAME, filename);
    }

    lua_current_script = NULL;
    lua_registered_script = NULL;

    lua_current_interpreter = luaL_newstate();

    if (lua_current_interpreter == NULL)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to create new "
                                         "sub-interpreter"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME);
        fclose (fp);
        return 0;
    }

#ifdef LUA_VERSION_NUM /* LUA_VERSION_NUM is defined only in lua >= 5.1.0 */
    luaL_openlibs (lua_current_interpreter);
#else
    luaopen_base (lua_current_interpreter);
    luaopen_string (lua_current_interpreter);
    luaopen_table (lua_current_interpreter);
    luaopen_math (lua_current_interpreter);
    luaopen_io (lua_current_interpreter);
    luaopen_debug (lua_current_interpreter);
#endif

    weechat_lua_register_lib (lua_current_interpreter, "weechat", weechat_lua_api_funcs);

#ifdef LUA_VERSION_NUM
    if (luaL_dostring (lua_current_interpreter, weechat_lua_code) != 0)
#else
    if (lua_dostring (lua_current_interpreter, weechat_lua_code) != 0)
#endif
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to redirect stdout "
                                         "and stderr"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME);
    }

    lua_current_script_filename = filename;

    if (luaL_loadfile (lua_current_interpreter, filename) != 0)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to load file \"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: error: %s"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME,
                        lua_tostring (lua_current_interpreter, -1));
        lua_close (lua_current_interpreter);
        fclose (fp);
        return 0;
    }

    if (lua_pcall (lua_current_interpreter, 0, 0, 0) != 0)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to execute file "
                                         "\"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: error: %s"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME,
                        lua_tostring (lua_current_interpreter, -1));
        lua_close (lua_current_interpreter);
        fclose (fp);

        /* if script was registered, remove it from list */
        if (lua_current_script)
        {
            plugin_script_remove (weechat_lua_plugin, &lua_scripts, &last_lua_script,
                                  lua_current_script);
        }

        return 0;
    }
    fclose (fp);

    if (!lua_registered_script)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: function \"register\" not "
                                         "found (or failed) in file \"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        lua_close (lua_current_interpreter);
        return 0;
    }
    lua_current_script = lua_registered_script;

    lua_current_script->interpreter = (lua_State *) lua_current_interpreter;

    /*
     * set input/close callbacks for buffers created by this script
     * (to restore callbacks after upgrade)
     */
    plugin_script_set_buffer_callbacks (weechat_lua_plugin,
                                        lua_scripts,
                                        lua_current_script,
                                        &weechat_lua_api_buffer_input_data_cb,
                                        &weechat_lua_api_buffer_close_cb);

    weechat_hook_signal_send ("lua_script_loaded", WEECHAT_HOOK_SIGNAL_STRING,
                              lua_current_script->filename);

    return 1;
}
Beispiel #30
0
int main (int argc, char* argv[])
{
 lua_State* L = lua_open();
    luaopen_base(L);
    luaopen_io(L);
    luaopen_string(L);
    luaopen_table(L);
    luaopen_math(L);
    luaopen_debug(L);

 lua_pushstring(L,TOLUA_VERSION); lua_setglobal(L,"TOLUA_VERSION");

 if (argc==1)
 {
  help();
  return 0;
 }
 else
 {
  int i, t;
  lua_newtable(L);
  lua_pushvalue(L,-1);
  lua_setglobal(L,"flags");
  t = lua_gettop(L);
  for (i=1; i<argc; ++i)
  {
   if (*argv[i] == '-')
   {
    switch (argv[i][1])
    {
     case 'v': version(); return 0;
     case 'h': help(); return 0;
     case 'p': setfield(L,t,"p",""); break;
     case 'P': setfield(L,t,"P",""); break;
     case 'o': setfield(L,t,"o",argv[++i]); break;
     case 'n': setfield(L,t,"n",argv[++i]); break;
     case 'H': setfield(L,t,"H",argv[++i]); break;
     case 'S': setfield(L,t,"S",""); break;
     case '1': setfield(L,t,"1",""); break;
     case 'L': setfield(L,t,"L",argv[++i]); break;
     default: error(argv[i]); break;
    }
   }
   else
   {
    setfield(L,t,"f",argv[i]);
    break;
   }
  }
  lua_pop(L,1);
 }
/* #define TOLUA_SCRIPT_RUN */
#ifndef TOLUA_SCRIPT_RUN
 {
  int tolua_tolua_open (lua_State* L);
  tolua_tolua_open(L);
 }
#else
 {
  char* p;
  char  path[BUFSIZ];
  strcpy(path,argv[0]);
  p = strrchr(path,'/');
  if (p==NULL) p = strrchr(path,'\\');
  p = (p==NULL) ? path : p+1;
  sprintf(p,"%s","../src/bin/lua/");
  lua_pushstring(L,path); lua_setglobal(L,"path");
        strcat(path,"all.lua");
  lua_dofile(L,path);
 }
#endif
 return 0;
}