static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index) { if (ls_lua_isboolean(L, index)) return list_append(list, ls_lua_toboolean(L, index) ? "true" : "false", 0); if (ls_lua_isnumber(L, index)) { LIST* newList; ls_lua_pushvalue(L, index); newList = list_append(list, ls_lua_tostring(L, -1), 0); ls_lua_pop(L, 1); return newList; } if (ls_lua_isstring(L, index)) { const char* value = ls_lua_tostring(L, index); return list_append(list, value, 0); } else if (ls_lua_istable(L, index)) { ls_lua_pushnil(L); while (ls_lua_next(L, index) != 0) { list = luahelper_addtolist(L, list, ls_lua_gettop(L)); ls_lua_pop(L, 1); } } return list; }
static LIST *luahelper_addtolist(ls_lua_State *L, LIST *list, int index) { if (ls_lua_isboolean(L, index)) return list_new(list, ls_lua_toboolean(L, index) ? "true" : "false", 0); if (ls_lua_isnumber(L, index)) return list_new(list, ls_lua_tostring(L, index), 0); if (ls_lua_isstring(L, index)) return list_new(list, ls_lua_tostring(L, index), 0); else if (ls_lua_istable(L, index)) { ls_lua_pushnil(L); while (ls_lua_next(L, index) != 0) { list = luahelper_addtolist(L, list, index + 2); ls_lua_pop(L, 1); } } return list; }
// jam_action(name, function, {options}) int LS_jam_action(ls_lua_State *L) { RULE* rule; const char* name; int paramIndex = 2; int numParams = ls_lua_gettop(L); if (numParams < 2) { //ls_luaL_error return 0; } if (!ls_lua_isstring(L, 1)) return 0; name = ls_lua_tostring(L, 1); rule = bindrule(name); if (rule->actions) { freestr(rule->actions); rule->actions = NULL; } if (rule->bindlist) { list_free(rule->bindlist); rule->bindlist = L0; } if (ls_lua_isstring(L, 2)) paramIndex = 2; else if (ls_lua_isstring(L, 3)) paramIndex = 3; else { return 0; } rule->actions = copystr(ls_lua_tostring(L, paramIndex)); rule->flags = 0; paramIndex = paramIndex == 2 ? 3 : 2; if (ls_lua_istable(L, paramIndex)) { ls_lua_getfield(L, paramIndex, "bind"); if (ls_lua_istable(L, -1)) { ls_lua_pushnil(L); while (ls_lua_next(L, -2) != 0) { if (!ls_lua_tostring(L, -1)) { printf("!!\n"); exit(1); } rule->bindlist = list_append(rule->bindlist, ls_lua_tostring(L, -1), 0); ls_lua_pop(L, 1); } ls_lua_pop(L, 1); } ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "updated"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_UPDATED : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "together"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_TOGETHER : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "ignore"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_IGNORE : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "quietly"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_QUIETLY : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "piecemeal"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_PIECEMEAL : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "existing"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_EXISTING : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "response"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_RESPONSE : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "lua"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_LUA : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "screenoutput"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_SCREENOUTPUT : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "removeemptydirs"); rule->flags |= ls_lua_toboolean(L, -1) ? RULE_REMOVEEMPTYDIRS : 0; ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "maxtargets"); if (ls_lua_isnumber(L, -1)) { rule->flags |= RULE_MAXTARGETS; rule->maxtargets = (int)ls_lua_tonumber(L, -1); } ls_lua_pop(L, 1); ls_lua_getfield(L, paramIndex, "maxline"); if (ls_lua_isnumber(L, -1)) { rule->flags |= RULE_MAXLINE; rule->maxline = (int)ls_lua_tonumber(L, -1); } ls_lua_pop(L, 1); } return 0; }