|
|
@ -6,12 +6,14 @@ |
|
|
|
#include <vsky/libdsp/lua/5.3.5/lauxlib.h> |
|
|
|
#include <vsky/libdsp/lua/5.3.5/lualib.h> |
|
|
|
|
|
|
|
#include <ti/ndk/inc/netmain.h> |
|
|
|
#include <ti/ndk/inc/nettools/inc/tftpif.h> |
|
|
|
#include <ti/ndk/inc/nettools/inc/inet.h> |
|
|
|
|
|
|
|
#include "script.h" |
|
|
|
|
|
|
|
#if !defined(LUA_PROMPT) |
|
|
|
#define LUA_PROMPT "> " |
|
|
|
#define LUA_PROMPT2 ">> " |
|
|
|
#endif |
|
|
|
|
|
|
|
#define LUA_MAXINPUT 512 |
|
|
|
|
|
|
@ -155,10 +157,8 @@ static int docall (lua_State *L, int narg, int nres) { |
|
|
|
** Prints an error message, adding the program name in front of it |
|
|
|
** (if present) |
|
|
|
*/ |
|
|
|
static void l_message (const char *pname, const char *msg, struct client *sock) |
|
|
|
static void l_message (const char *msg, struct client *sock) |
|
|
|
{ |
|
|
|
if (pname) |
|
|
|
con_printf(sock, "%s: ", pname); |
|
|
|
con_printf(sock, "%s\n", msg); |
|
|
|
} |
|
|
|
|
|
|
@ -173,11 +173,20 @@ static void l_print (lua_State *L, struct client *sock) |
|
|
|
lua_getglobal(L, "print"); |
|
|
|
lua_insert(L, 1); |
|
|
|
if (lua_pcall(L, n, 0, 0) != LUA_OK) |
|
|
|
l_message("lua", lua_pushfstring(L, "error calling 'print' (%s)", |
|
|
|
l_message(lua_pushfstring(L, "error calling 'print' (%s)", |
|
|
|
lua_tostring(L, -1)), sock); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static int report (lua_State *L, int status, struct client *sock) { |
|
|
|
if (status != LUA_OK) { |
|
|
|
const char *msg = lua_tostring(L, -1); |
|
|
|
l_message( msg, sock); |
|
|
|
lua_pop(L, 1); /* remove message */ |
|
|
|
} |
|
|
|
return status; |
|
|
|
} |
|
|
|
|
|
|
|
static int __do_print(lua_State *L) |
|
|
|
{ |
|
|
|
struct client *sock = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
@ -211,28 +220,49 @@ static int __byebye(lua_State *L) |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
int script_repl(lua_State *L, struct client *client) |
|
|
|
static int __tftp(lua_State *L) |
|
|
|
{ |
|
|
|
int status; |
|
|
|
static struct luaL_Reg meths[] = { |
|
|
|
unsigned int addr = luaL_checkinteger(L, 1); |
|
|
|
const char *file = luaL_checkstring(L, 2); |
|
|
|
const char *ip = luaL_checkstring(L, 3); |
|
|
|
unsigned int uip, sz = 0; |
|
|
|
unsigned short code; |
|
|
|
|
|
|
|
if (lua_gettop(L) >= 4) |
|
|
|
sz = luaL_checkinteger(L, 4); |
|
|
|
uip = inet_addr(ip); |
|
|
|
if (NtTftpRecv(uip, (char *)file, (void *)addr, &sz, &code) >= 0) { |
|
|
|
lua_pushboolean(L, 1); |
|
|
|
lua_pushinteger(L, sz); |
|
|
|
} else { |
|
|
|
lua_pushboolean(L, 0); |
|
|
|
lua_pushinteger(L, code); |
|
|
|
} |
|
|
|
return 2; |
|
|
|
} |
|
|
|
|
|
|
|
static __add_builtins(lua_State *L, struct client *sock) |
|
|
|
{ |
|
|
|
static struct luaL_Reg meth[] = { |
|
|
|
{"print", __do_print}, |
|
|
|
{"bye", __byebye}, |
|
|
|
{"logout", __byebye}, |
|
|
|
{"bye", __byebye}, |
|
|
|
{"tftp", __tftp}, |
|
|
|
{NULL, NULL} |
|
|
|
}; |
|
|
|
|
|
|
|
lua_pushlightuserdata(L, client); |
|
|
|
lua_pushcclosure(L, __do_print, 1); |
|
|
|
lua_setglobal(L, "print"); |
|
|
|
}; |
|
|
|
const luaL_Reg *lib; |
|
|
|
|
|
|
|
for (lib = meth; lib->func; ++lib) { |
|
|
|
lua_pushlightuserdata(L, sock); |
|
|
|
lua_pushcclosure(L, lib->func, 1); |
|
|
|
lua_setglobal(L, lib->name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
lua_pushlightuserdata(L, client); |
|
|
|
lua_pushcclosure(L, __byebye, 1); |
|
|
|
lua_setglobal(L, "bye"); |
|
|
|
int script_repl(lua_State *L, struct client *client) |
|
|
|
{ |
|
|
|
int status; |
|
|
|
|
|
|
|
lua_newtable(L); |
|
|
|
lua_pushlightuserdata(L, client); |
|
|
|
luaL_setfuncs(L, meths, 1); |
|
|
|
lua_setglobal(L, "telnet"); |
|
|
|
__add_builtins(L, client); |
|
|
|
|
|
|
|
do { |
|
|
|
status = loadline(L, client); |
|
|
@ -241,8 +271,10 @@ int script_repl(lua_State *L, struct client *client) |
|
|
|
status = docall(L, 0, LUA_MULTRET); |
|
|
|
if (status == LUA_OK) |
|
|
|
l_print(L, client); |
|
|
|
else |
|
|
|
else { |
|
|
|
report(L, status, client); |
|
|
|
con_printf(client, "\r\n"); |
|
|
|
} |
|
|
|
|
|
|
|
} while (!con_bye(client)); |
|
|
|
|
|
|
|