forked from cvra/nastya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_console.c
190 lines (155 loc) · 4.44 KB
/
lua_console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <lwip/sockets.h>
#include <lwip/api.h>
#include <lwip/inet.h>
#include <stdlib.h>
#include <string.h>
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
#define MAX_COMMAND_LEN 200
#define PROMPT ">> "
// simple example of function binding from C to lua
int mysum(lua_State *l)
{
if (lua_gettop(l) != 2 || !lua_isnumber(l, -1) || !lua_isnumber(l, -2))
return 0;
double a, b;
// first parameters are top of stack
a = lua_tonumber(l, -1);
b = lua_tonumber(l, -2);
lua_pushnumber(l, a+b);
return 1; // return value count
}
int print_func(lua_State *l)
{
struct netconn *conn;
const char *to_print;
if (lua_gettop(l) == 0)
return 0;
lua_getglobal(l, "__conn");
conn = lua_touserdata(l, -1);
lua_pop(l, 1);
if (lua_isstring(l, -1)) {
to_print = lua_tostring(l, -1);
netconn_write(conn, to_print, strlen(to_print), NETCONN_COPY);
netconn_write(conn, "\r\n", 2, NETCONN_COPY);
} else {
const char *error = "ERROR : cannot print that\r\n";
netconn_write(conn, error, strlen(error), NETCONN_COPY);
}
return 0;
}
//void lua_do_rom_script(lua_State *l, char *buffer, int size)
//{
// int i;
//
// unsigned char *command = malloc(size + 1);
//
//
// for (i=0;i<size;i++) {
// command[i] = buffer[i];
// }
// command[i] = 0;
//
// luaL_dostring(l, command);
//
// free(command);
//}
//
//void lua_prepare_shell(lua_State *l)
//{
// extern unsigned char commands_lua[];
// extern long int commands_lua_size;
//
//
// lua_do_rom_script(l, commands_lua, commands_lua_size);
//}
//
//void lua_do_settings(void)
//{
// extern unsigned char settings_lua[];
// extern long int settings_lua_size;
//
// lua_State *l = luaL_newstate();
// luaL_openlibs(l);
//
// commands_register(l);
//
// lua_prepare_shell(l);
//
// lua_do_rom_script(l, settings_lua, settings_lua_size);
//}
void serve_conn(struct netconn *conn)
{
struct netbuf *buf;
void *data;
u16_t len;
err_t err;
lua_State *l;
static char command[MAX_COMMAND_LEN], real_command[MAX_COMMAND_LEN];
int ret;
l = luaL_newstate();
luaL_openlibs(l);
lua_pushlightuserdata(l, conn);
lua_setglobal(l, "__conn");
lua_pushcfunction(l, print_func);
lua_setglobal(l, "print");
commands_register(l);
// lua_prepare_shell(l);
netconn_write(conn, PROMPT, strlen(PROMPT), NETCONN_COPY);
while((err = netconn_recv(conn, &buf)) == ERR_OK) {
do {
/* Copies the buffer data into a string. */
netbuf_data(buf, &data, &len);
if (len >= MAX_COMMAND_LEN - 7) { // CMD_LEN - strlen("print()")
const char *msg = "command too long\n\r";
netconn_write(conn, msg, strlen(msg), NETCONN_COPY);
}
strncpy(command, data, len);
command[len] = 0;
sprintf(real_command, "print(%s)", command);
luaL_loadstring(l, real_command);
ret = lua_pcall(l, 0, LUA_MULTRET, 0);
if (ret) {
lua_pop(l, 1);
luaL_loadstring(l, command);
ret = lua_pcall(l, 0, LUA_MULTRET, 0);
if (ret) {
snprintf(command, MAX_COMMAND_LEN, "ERROR %d : %s\n", ret, lua_tostring(l, -1));
netconn_write(conn, command, strlen(command), NETCONN_COPY);
lua_pop(l, 1);
}
}
netconn_write(conn, PROMPT, strlen(PROMPT), NETCONN_COPY);
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
lua_close(l);
}
static void luaconsole_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
/* Bind connection to well known port number 7. */
netconn_bind(conn, NULL, 88);
/* Tell connection to go into listening mode. */
netconn_listen(conn);
printf("lua shell started\n");
while(1) {
/* Grab new connection. */
err = netconn_accept(conn, &newconn);
printf("lua shell connection accepted\n");
if(err == ERR_OK) {
serve_conn(newconn);
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
void luaconsole_init(void)
{
sys_thread_new("luaconsole",luaconsole_thread , NULL, DEFAULT_THREAD_STACKSIZE, 32);
}