diff options
author | William Wilgus <me.theuser@yahoo.com> | 2020-05-29 10:44:24 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2020-06-01 13:23:38 -0500 |
commit | e4ee5980c6490c6e4c5cedb8b1b4c37443b1c35a (patch) | |
tree | 150b6a3cd8707962552929ca1b02e71a63858e26 /apps/plugins | |
parent | fba4df5d6d4317e02f1baf25b54c2c051eec5d1d (diff) | |
download | rockbox-e4ee5980c6490c6e4c5cedb8b1b4c37443b1c35a.tar.gz rockbox-e4ee5980c6490c6e4c5cedb8b1b4c37443b1c35a.zip |
lua add _fullpath and argument parsing
WIP for passing arguments to lua scripts
args are recognized by adding ?arg to the end of the script path
The easiest way to do this currently is to use the function rb.restart_lua
local sfile = rawget(_G, "_fullpath") or ""
local sArgs = rawget(_G, "_arguments")
if not sArgs then
rb.restart_lua(sfile .. "?my arguments")
else
rb.splash(1000, sfile .. "?" .. sArgs)
end
I'd eventually like to figure out a sensible way to do this from a
shortcut / quick list
Change-Id: I2b60fe3b8f1d04b57361fe532510bd6afee59fbf
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/lua/rocklua.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c index ace73ee449..79f6bae953 100644 --- a/apps/plugins/lua/rocklua.c +++ b/apps/plugins/lua/rocklua.c | |||
@@ -146,12 +146,28 @@ static int docall (lua_State *L) { | |||
146 | } | 146 | } |
147 | 147 | ||
148 | static void lua_atexit(void); | 148 | static void lua_atexit(void); |
149 | static int lua_split_arguments(lua_State *L, const char *filename); | ||
150 | |||
149 | static int loadfile_newstate(lua_State **L, const char *filename) | 151 | static int loadfile_newstate(lua_State **L, const char *filename) |
150 | { | 152 | { |
151 | *L = luaL_newstate(); | 153 | const char *file; |
152 | rb_atexit(lua_atexit); | 154 | int ret; |
153 | rocklua_openlibs(*L); | 155 | |
154 | return luaL_loadfile(*L, filename); | 156 | *L = luaL_newstate(); |
157 | rb_atexit(lua_atexit); | ||
158 | |||
159 | lua_gc(*L, LUA_GCSTOP, 0); /* stop collector during initialization */ | ||
160 | rocklua_openlibs(*L); | ||
161 | |||
162 | lua_split_arguments(*L, filename); | ||
163 | lua_setglobal (*L, "_arguments"); | ||
164 | file = lua_tostring (*L, -1); | ||
165 | lua_setglobal (*L, "_fullpath"); | ||
166 | /* lua manual -> no guarantee pointer valid after value is removed from stack */ | ||
167 | ret = luaL_loadfile(*L, file); | ||
168 | lua_gc(*L, LUA_GCRESTART, 0); | ||
169 | |||
170 | return ret; | ||
155 | } | 171 | } |
156 | 172 | ||
157 | static void lua_atexit(void) | 173 | static void lua_atexit(void) |
@@ -162,10 +178,14 @@ static void lua_atexit(void) | |||
162 | { | 178 | { |
163 | if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */ | 179 | if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */ |
164 | { | 180 | { |
165 | filename = (char *) malloc(MAX_PATH); | 181 | filename = (char *) malloc((MAX_PATH * 2) + 1); |
166 | 182 | ||
167 | if (filename) /* out of memory? */ | 183 | if (filename) {/* out of memory? */ |
168 | rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH); | 184 | filename[MAX_PATH * 2] = '\0'; |
185 | rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH * 2); | ||
186 | } else { | ||
187 | goto ERR_RUN; | ||
188 | } | ||
169 | lua_close(Ls); /* close old state */ | 189 | lua_close(Ls); /* close old state */ |
170 | 190 | ||
171 | lu_status = loadfile_newstate(&Ls, filename); | 191 | lu_status = loadfile_newstate(&Ls, filename); |
@@ -175,6 +195,7 @@ static void lua_atexit(void) | |||
175 | } | 195 | } |
176 | else if (lua_tointeger(Ls, -1) != 0) /* os.exit */ | 196 | else if (lua_tointeger(Ls, -1) != 0) /* os.exit */ |
177 | { | 197 | { |
198 | ERR_RUN: | ||
178 | lu_status = LUA_ERRRUN; | 199 | lu_status = LUA_ERRRUN; |
179 | lua_pop(Ls, 1); /* put exit string on top of stack */ | 200 | lua_pop(Ls, 1); /* put exit string on top of stack */ |
180 | plugin_start(NULL); | 201 | plugin_start(NULL); |
@@ -185,6 +206,25 @@ static void lua_atexit(void) | |||
185 | _exit(0); /* don't call exit handler */ | 206 | _exit(0); /* don't call exit handler */ |
186 | } | 207 | } |
187 | 208 | ||
209 | /* split filename at argchar | ||
210 | * remainder of filename pushed on stack (-1) | ||
211 | * argument string pushed on stack or nil if doesn't exist (-2) | ||
212 | */ | ||
213 | static int lua_split_arguments(lua_State *L, const char *filename) | ||
214 | { | ||
215 | const char argchar = '?'; | ||
216 | const char* arguments = strchr(filename, argchar); | ||
217 | if(arguments) { | ||
218 | lua_pushstring(L, (arguments + 1)); | ||
219 | } else { | ||
220 | arguments = strlen(filename) + filename; | ||
221 | lua_pushnil(L); | ||
222 | } | ||
223 | lua_pushlstring(L, filename, arguments - filename); | ||
224 | lua_insert(L, -2); /* swap filename and argument */ | ||
225 | return 2; | ||
226 | } | ||
227 | |||
188 | /***************** Plugin Entry Point *****************/ | 228 | /***************** Plugin Entry Point *****************/ |
189 | enum plugin_status plugin_start(const void* parameter) | 229 | enum plugin_status plugin_start(const void* parameter) |
190 | { | 230 | { |