summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-10-23 10:38:20 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-10-23 10:38:20 +0000
commit9dd25adae488ef5461f2cf6fabb25ac974703be4 (patch)
tree8370aed42deb4887c10b89e4fff1a27f1137b578 /apps
parentff8e9d95ee1f33b9e5fba10626649984853a8a7c (diff)
downloadrockbox-9dd25adae488ef5461f2cf6fabb25ac974703be4.tar.gz
rockbox-9dd25adae488ef5461f2cf6fabb25ac974703be4.zip
Lua: add do_menu() wrapper. Also fix potential NULL pointer dereference
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23320 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/lua/rocklib.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index c6b399c8a5..cb90eddcd6 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -503,6 +503,8 @@ static void fill_text_message(lua_State *L, struct text_message * message,
503 luaL_checktype(L, pos, LUA_TTABLE); 503 luaL_checktype(L, pos, LUA_TTABLE);
504 int n = luaL_getn(L, pos); 504 int n = luaL_getn(L, pos);
505 const char **lines = (const char**) dlmalloc(n * sizeof(const char*)); 505 const char **lines = (const char**) dlmalloc(n * sizeof(const char*));
506 if(lines == NULL)
507 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*));
506 for(i=1; i<=n; i++) 508 for(i=1; i<=n; i++)
507 { 509 {
508 lua_rawgeti(L, pos, i); 510 lua_rawgeti(L, pos, i);
@@ -536,6 +538,41 @@ RB_WRAP(gui_syncyesno_run)
536 return 1; 538 return 1;
537} 539}
538 540
541RB_WRAP(do_menu)
542{
543 struct menu_callback_with_desc menu_desc = {NULL, NULL, Icon_NOICON};
544 struct menu_item_ex menu = {MT_RETURN_ID | MENU_HAS_DESC, {.strings = NULL},
545 {.callback_and_desc = &menu_desc}};
546 int i, n, start_selected;
547 const char **items, *title;
548
549 title = luaL_checkstring(L, 1);
550 luaL_checktype(L, 2, LUA_TTABLE);
551 start_selected = luaL_optint(L, 3, 0);
552
553 n = luaL_getn(L, 2);
554 items = (const char**) dlmalloc(n * sizeof(const char*));
555 if(items == NULL)
556 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*));
557 for(i=1; i<=n; i++)
558 {
559 lua_rawgeti(L, 2, i); /* Push item on the stack */
560 items[i-1] = luaL_checkstring(L, -1);
561 lua_pop(L, 1); /* Pop it */
562 }
563
564 menu.strings = items;
565 menu.flags |= MENU_ITEM_COUNT(n);
566 menu_desc.desc = (unsigned char*) title;
567
568 int result = rb->do_menu(&menu, &start_selected, NULL, false);
569
570 dlfree(items);
571
572 lua_pushinteger(L, result);
573 return 1;
574}
575
539#define R(NAME) {#NAME, rock_##NAME} 576#define R(NAME) {#NAME, rock_##NAME}
540static const luaL_Reg rocklib[] = 577static const luaL_Reg rocklib[] =
541{ 578{
@@ -575,6 +612,7 @@ static const luaL_Reg rocklib[] =
575 R(clear_viewport), 612 R(clear_viewport),
576 R(current_path), 613 R(current_path),
577 R(gui_syncyesno_run), 614 R(gui_syncyesno_run),
615 R(do_menu),
578 616
579 {"new_image", rli_new}, 617 {"new_image", rli_new},
580 618