Exemplo n.º 1
0
static int lxc_cb_lua(char *word[], char *word_eol[], void *userdata)
{
	lua_State *L = lxc_new_state();
	if (word[2][0] == '\0') {
		xchat_printf(ph, "LUA: Usage: /LUA LUA_CODE... execute LUA_CODE");
		return XCHAT_EAT_ALL;
	}
	if (luaL_loadbuffer(L, word_eol[2], strlen(word_eol[2]), "/LUA")) {
		xchat_printf(ph, "LUA: error loading line %s", lua_tostring(L, -1));
		lua_pop(L, 1);
	}

#define LXC_HOOK_DISABLE "xchat.hook_command = nil\n" \
								 "xchat.hook_server  = nil\n" \
								 "xchat.hook_print   = nil\n" \
								 "xchat.hook_timer   = nil\n"

#if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM >= 501)
	(void)luaL_dostring(L, LXC_HOOK_DISABLE);
#else
	lua_dostring(L, LXC_HOOK_DISABLE);
#endif

	if (lua_pcall(L, 0, 0, 0)) {
		xchat_printf(ph, "LUA: error executing line %s", lua_tostring(L, -1));
		lua_pop(L, 1);
	}

	lua_close(L);
	return XCHAT_EAT_ALL;
}
Exemplo n.º 2
0
static void internaldostring() {
	if (lua_getparam(2) != LUA_NOOBJECT)
		lua_error("invalid 2nd argument (probably obsolete code)");
	if (lua_dostring(luaL_check_string(1)) == 0)
		if (luaA_passresults() == 0)
			lua_pushuserdata(0);  // at least one result to signal no errors
}
Exemplo n.º 3
0
/*
** Internal function: do a string
*/
void lua_internaldostring (void)
{
 lua_Object obj = lua_getparam (1);
 if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
  lua_pushnumber(1);
 else
  lua_pushnil();
}
Exemplo n.º 4
0
void ScriptManager::executeString(const Common::UString &code) {
	assert(_luaState && _regNestingLevel == 0);

	const int execResult = lua_dostring(_luaState, code.c_str());
	if (execResult != 0) {
		throw Common::Exception("Failed to execute Lua code: %s", code.c_str());
	}
}
Exemplo n.º 5
0
Arquivo: liolib.c Projeto: jeske/hz
static void io_debug (void)
{
  while (1) {
    char buffer[250];
    fprintf(stderr, "lua_debug> ");
    if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
    if (strcmp(buffer, "cont\n") == 0) return;
    lua_dostring(buffer);
  }
}
Exemplo n.º 6
0
static void manual_input (void)
{
  if (isatty(fileno(stdin)))
  {
   char buffer[250];
   while (gets(buffer) != 0)
     lua_dostring(buffer);
  }
  else
    lua_dofile(NULL);  /* executes stdin as a file */
}
Exemplo n.º 7
0
static void manual_input (void)
{
    if (isatty(0))
    {
        char buffer[250];
        while (fgets(buffer, sizeof(buffer), stdin) != 0)
            lua_dostring(buffer);
    }
    else
        lua_dofile(NULL);  /* executes stdin as a file */
}
Exemplo n.º 8
0
static int io_debug (lua_State *L) {
  for (;;) {
    char buffer[250];
    fprintf(stderr, "lua_debug> ");
    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
        strcmp(buffer, "cont\n") == 0)
      return 0;
    lua_dostring(L, buffer);
    lua_settop(L, 0);  /* remove eventual returns */
  }
}
Exemplo n.º 9
0
int InitScripting(DISZ80 *d)
{
	char	buf[256];
	int		err;

	if (d->ls == NULL)
		{
		d->ls = lua_open(0);

		if (d->ls == NULL)
			return DERR_OUTOFMEM;
		
		lua_baselibopen(d->ls);
		lua_strlibopen(d->ls);

		lua_pushuserdata(d->ls, d);
		lua_setglobal(d->ls, "DISZ80STRUC");

		lua_register(d->ls, LUA_ERRORMESSAGE, LuaErrorHandler);

/* Register the dZ80 support functions */
		lua_register(d->ls, "d_RegTrap",		d_RegTrap);
		lua_register(d->ls, "d_AddComment", 	d_AddComment);
		lua_register(d->ls, "d_AddToDis",		d_AddToDis);
		lua_register(d->ls, "d_AddToDisTab",	d_AddToDisTab);
		lua_register(d->ls, "d_GetByte",		d_GetByte);
		lua_register(d->ls, "d_LookByte",		d_LookByte);
		lua_register(d->ls, "d_DB", 			d_DB);
		lua_register(d->ls, "d_FlushLine",		d_FlushLine);
		lua_register(d->ls, "d_IsCodeByte",		d_IsCodeByte);
		lua_register(d->ls, "d_GetPC",			d_GetPC);
		lua_register(d->ls, "d_LookByteAddr",	d_LookByteAddr);
		lua_register(d->ls, "d_GetPass",		d_GetPass);
		lua_register(d->ls, "d_Message",		d_Message);

/* Register the general support functions */
		lua_register(d->ls, "hex",				d_FromHex);

/* Set up the trap registration functions */
		sprintf(buf,
			"__dz80_pre={}\n"
			"__dz80_post={}\n"
			"function d_PreTrap(op, fn) __dz80_pre[op]=fn d_RegTrap(op, %d) end\n"
			"function d_PostTrap(op, fn) __dz80_post[op]=fn d_RegTrap(op, %d) end\n",
			D_SCRIPT_PRE, D_SCRIPT_POST);

		err = lua_dostring(d->ls, buf);
		if (err)
			return DERR_SCRIPTERROR;
		}

	return DERR_NONE;
}
Exemplo n.º 10
0
int cboxlua_open (void)
{
  lua_register("iupCreateCbox",CreateCbox);

  return lua_dostring("IUPCBOX = {parent = IUPCANVAS}\n"
                      "function IUPCBOX:CreateIUPelement (obj)\n"
                      "  return iupCreateCbox (obj, getn(obj))\n"
                      "end\n"
                      "function iupcbox (o)\n"
                      "  return IUPCBOX:Constructor (o)\n"
                      "end\n"
                      "iup.cbox = iupcbox\n");
}
Exemplo n.º 11
0
/* a really simple way of calling lua from C
 just give it a lua state & a string to execute
Unfortunately lua keeps changing its API's.
In lua 5.0.X it's lua_dostring()
In lua 5.1.X it's luaL_dostring()
so we have a few extra compiles
*/
int dostring(lua_State *L, char* str) {
  int ok;
#if (defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM>=501))

  ok=luaL_dostring(L,str);	/* looks like this is lua 5.1.X or later, good */
#else

  ok=lua_dostring(L,str);	/* might be lua 5.0.x, using lua_dostring */
#endif

  if (ok!=0)
    printf("[C] ERROR in dostring: %s\n",lua_tostring(L,-1));
  return ok;
}
Exemplo n.º 12
0
int	main()
{
	config::open();

	luaSDL_initialize(config::L);

	lua_dofile(config::L, "property.lua");	// migrate this into config::open().
	lua_dofile(config::L, "init.lua");

	lua_register(config::L, "fc", float_compare_lua);
	lua_register(config::L, "fasi", float_as_int_lua);

	// Show initial test var values.
	printf( "test_cfloat = %f, test_cvar = %s\n",
			(float) test_cfloat,
			(const char*) test_cvar );

	// Change the values, and show the results.
	test_cfloat = 100.5f;
	test_cvar = "newfilename.jpg";
	printf( "test_cfloat = %f, test_cvar = %s\n", (float) test_cfloat, (const char*) test_cvar );

	// Interpreter loop, to fiddle with cfloats & cvars...
	for (;;) {
		// Get a command line.
		char	buffer[1000];
		printf( "> " );
		fgets( buffer, 1000, stdin );

		if ( strstr( buffer, "quit" ) ) {
			break;
		}
		
		// Execute it.
		lua_dostring( config::L, buffer );
	}

	// Show ending var values.
	printf( "test_cfloat = %f, test_cvar = %s, test_cvalue = %s\n", (float) test_cfloat, (const char*) cvar("test_cvar"), (const char*) c10 );

	printf("actorprefs.jumpheight = %f, actorprefs.runspeed = %f, actorprefs.color = %s",
		   (float) actorprefs::jumpheight,
		   (float) actorprefs::runspeed,
		   (const char*) actorprefs::color
		);

	return 0;
}
Exemplo n.º 13
0
void LuaNetworkClient::ProcessPackets()
{
	if (!m_socket.IsValid())
		return;

	EnterCriticalSection( &m_socketAccessCS );

	while (m_socket.IsReadDataWaiting())
	{
		SimpleString command;
		if (ReadString(m_socket, command))
		{
			lua_dostring(m_state, command);
		}
	}

	LeaveCriticalSection( &m_socketAccessCS );
}
Exemplo n.º 14
0
double CPromptBox::ParseDouble()
{
	// parse the contents of the edit box and return a double;

	CString tolua;
	int i,k;
	double x=0;

	tolua="return " + instring;
	i=lua_gettop(lua);
	lua_dostring(lua,tolua);
	k=lua_gettop(lua);
	if (i!=k){
		tolua=lua_tostring(lua,-1);
		if (tolua.GetLength()==0) AfxMessageBox("Input does not evaluate to a numerical value");
		else x=lua_todouble(lua,-1);
	}		
	return x;
}
Exemplo n.º 15
0
void CLuaConsoleDlg::OnBnClickedEvaluate()
{
	if (bLinehook!=FALSE) return;
	
	CString LuaCmd;

	UpdateData();
	ToOutput(inbuffer+"\r\n");
	LuaCmd=inbuffer;
	inbuffer="";
	UpdateData(FALSE);
	bLinehook=NormalLua;

	CStatusBar *StatBar=(CStatusBar *)((CFrameWnd *)GetTopLevelFrame())->GetMessageBar();
		StatBar->SetPaneText(0,"EXECUTING LUASCRIPT -- HIT <ESC> TO ABORT",TRUE);
	int lua_error_code=lua_dostring(lua,LuaCmd);
	
	if(lua_error_code!=FALSE){
//		if (lua_error_code==LUA_ERRRUN)
//			AfxMessageBox("Run Error");
//		if (lua_error_code==LUA_ERRSYNTAX)
//			AfxMessageBox("Syntax Error");
		if (lua_error_code==LUA_ERRMEM)
			AfxMessageBox("Lua memory Error");
		if (lua_error_code==LUA_ERRERR)
			AfxMessageBox("User error error");
		if (lua_error_code==LUA_ERRFILE)
			AfxMessageBox("File Error");
		inbuffer=LuaCmd;
		UpdateData(FALSE);
	}

	bLinehook=FALSE;
	inbufferctrl.SetFocus();
	StatBar->SetPaneText(0,"Ready",TRUE);
	
	if(lua_byebye==TRUE){
		ASSERT(AfxGetMainWnd() != NULL);
		AfxGetMainWnd()->PostMessage(WM_CLOSE);
	}


}
Exemplo n.º 16
0
/*
** Python prototype: "lua_state <-- lua_open([stack_size])"
** opens a Lua State and returns it to Python
** accepts an optional parameter informing the size of the stack Lua should use
** (measured in number of elements)
*/
static PyObject *Py_lua_open(PyObject *self, PyObject *args) {
lua_State *L;
int stack_size = 0;
PyObject *result = NULL;

   if (PyArg_ParseTuple(args, "|i:lua_open", &stack_size)) {
      L = lua_open(stack_size);
      lua_baselibopen(L);
      lua_iolibopen(L);
      lua_strlibopen(L);
      lua_mathlibopen(L);
      lua_dblibopen(L);

      lua_register(L, "_LuaPy_callPythonFunction", Lua_callPythonFunction);
      lua_register(L, "_LuaPy_setErrorMessage",    Lua_setErrorMessage);
      lua_dostring(L, LUA_init_state);
      result = Py_BuildValue("i", L);
   }
   return result;
}
Exemplo n.º 17
0
static int read_number (lua_State *L, FILE *f) {
//  double d;
//  if (fscanf(f, "%lf", &d) == 1) {
//    lua_pushnumber(L, d);
//    return 1;
//  }
//  else return 0;  /* read fails */
//}
  char v[256],s[256];
  int i,k;

  if (fscanf(f,"%s",v)>0){
	 sprintf(s,"return %s",v);
	 i=lua_gettop(L);
	 lua_dostring(L,s);
	 k=lua_gettop(L);
	 return (k-i);
  }
  else return 0;  /* read fails */
}
Exemplo n.º 18
0
/*
** Python prototype: "<tuple|value> <-- lua(lua_code)"
** executes the Lua "dostring" function, returning all return values to Python.
** The Lua return values can be of the type "number", "string", "userdata" or
** "python_function" only (the others are not yet implemented).
** If only 1 value is returned from Lua, it is passed as a single value to
** Python, but if Lua returns 0 or more than 1 values, a tuple is passed to
** Python.
*/
static PyObject *Py_lua(PyObject *self, PyObject *args) {
lua_State *L;
char *lua_code;
int lua_return_code;
int nreturns;
PyObject *result;

   if (PyArg_ParseTuple(args, "is:lua", &L, &lua_code)) {
      lua_return_code = lua_dostring(L, lua_code);
      /* treat errors that Lua can't give its own message */
      if (lua_return_code == LUA_ERRMEM) {
         PyErr_NoMemory();
         return NULL;
      } else if (lua_return_code == LUA_ERRERR) {
         PyErr_SetString(PyExc_Exception, "LuaPy: error in the user-defined Lua error function!");
         return NULL;
      } else if (lua_return_code != 0) {
         /* Lua error ocurred. Set error condition in Python */
         /* (Lua has already told Python what happened -- via 'Lua_setErrorMessage') */
         return NULL;
      }

      nreturns = lua_gettop(L);
      result = pyTryValue(luaStack_to_pythonTuple(L, 1, nreturns));
      /* remove Lua return values from the stack */
      lua_pop(L, nreturns);

   } else {
      return NULL;
   }

   /* watch out the 'LuaPy_Python_error_ocurred' condition, which can change due to */
   /* the execution of 'lua_dostring' */
   if (LuaPy_Python_error_ocurred == False) {
      return result;
   } else {
      return NULL;
   }
}
Exemplo n.º 19
0
bool kgmLuaScript::load(kgmString s)
{
  if (!resources)
    return false;

  kgmString ls = "scripts/";

  ls += (s + ".lua");

  kgmString script;

  kgmMemory<u8> content;

  if (!resources->getFile(ls, content))
    return false;

  script = (const s8*) content.data();

  if(lua_dostring(handler, script.data()))
    return false;

  return true;
}
Exemplo n.º 20
0
int main (int argc, char *argv[])
{
 int i;
 int result = 0;
 iolib_open ();
 strlib_open ();
 mathlib_open ();

 lua_register("argv", lua_getargv);

 if (argc < 2)
 {
   char buffer[250];
   while (gets(buffer) != 0)
     result = lua_dostring(buffer);
 }
 else
 {
  for (i=1; i<argc; i++)
  {
   if (strcmp(argv[i], "--") == 0)
   {
    lua_argc = argc-i-1;
    lua_argv = argv+i;
    break;
   }
  }
  for (i=1; i<argc; i++)
  {
   if (strcmp(argv[i], "--") == 0)
    break;
   else
    result = lua_dofile (argv[i]);
  }
 }
 return result;
}
Exemplo n.º 21
0
extern int lua_dostring(const char* str) {
	return lua_dostring(gL, str);
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
0
bool kgmLuaRun(kgmString str){
  if(lua_dostring(lua_main, str.data()))
    return false;

  return true;
}
Exemplo n.º 24
0
static int luaopen_xchat(lua_State *L)
{
	int i;
/* 
 * wrappers for xchat.printf() and xchat.commandf() 
 * ... xchat.strip 
 */
#define LXC_WRAPPERS  "function xchat.printf(...)\n" \
						 "    xchat.print(string.format(unpack(arg)))\n" \
						 "end\n" \
						 "function xchat.commandf(...)\n" \
						 "    xchat.command(string.format(unpack(arg)))\n" \
						 "end\n" \
						 "function xchat.strip(str, flags)\n" \
						 "    if flags == nil then\n" \
						 "        flags = xchat.STRIP_ALL\n" \
						 "    end\n" \
						 "    local bits = xchat.bits(flags)\n" \
						 "    if bits[1] then\n" \
						 "        str = string.gsub(\n" \
						 "            string.gsub(str, \"\\3%d%d?,%d%d?\", \"\"),\n" \
						 "                \"\\3%d%d?\", \"\")\n" \
						 "    end\n" \
						 "    if bits[2] then\n" \
						 "        -- bold, beep, reset, reverse, underline\n" \
						 "        str = string.gsub(str,\n" \
						 "            \"[\\2\\7\\15\\22\\31]\", \"\")\n" \
						 "    end\n" \
						 "    return str\n" \
						 "end\n"

#if defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM >= 501)
	luaL_register(L, "xchat", lxc_functions);
	(void)luaL_dostring(L, LXC_WRAPPERS);
#else
	luaL_openlib(L, "xchat", lxc_functions, 0);
	lua_dostring(L, LXC_WRAPPERS);
#endif
	
	luaL_newmetatable(L, "xchat");

	lua_pushliteral(L, "__index");
	lua_newtable(L); 

	lua_pushstring(L, "ARCH");
#ifdef _WIN32
	lua_pushstring(L, "Windows");
#else
	lua_pushstring(L, "Unix");
#endif
	lua_settable(L, -3); /* add to table __index */

	for (i=0; lxc_consts[i].name; i++) {
		lua_pushstring(L, lxc_consts[i].name);
		lua_pushnumber(L, lxc_consts[i].value);
		lua_settable(L, -3); /* add to table __index */
	}
	lua_settable(L, -3); /* add to metatable */

	lua_pushliteral(L, "__newindex");   
	lua_pushcfunction(L, lxc__newindex); 
	lua_settable(L, -3);                
/* 
	lua_pushliteral(L, "__metatable");
	lua_pushstring(L, "nothing to see here, move along");
	lua_settable(L, -3);               
*/
	lua_setmetatable(L, -2);
	lua_pop(L, 1);
	return 1;
}
Exemplo n.º 25
0
int scripting_Run(const char *command) {
  /* fprintf(stderr, "[command] %s\n", command); */
  return lua_dostring(L, command);
}