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; }
int luahelper_taskadd(const char* taskscript) { int ret; int ref; size_t taskscriptlen = strlen(taskscript); char* newTaskScript; ls_lua_init(); ls_lua_getfield(L, LUA_GLOBALSINDEX, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "gen"); /* lanes gen */ ls_lua_pushstring(L, "*"); /* lanes gen * */ newTaskScript = malloc( taskscriptlen + 1 ); strncpy(newTaskScript, taskscript, taskscriptlen); newTaskScript[taskscriptlen] = 0; ret = ls_luaL_loadstring(L, newTaskScript); /* lanes gen * script */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); printf("%s\n", newTaskScript); free(newTaskScript); return -1; } free(newTaskScript); ret = ls_lua_pcall(L, 2, 1, 0); /* lanes lane_h */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } if (!ls_lua_isfunction(L, -1)) /* lanes lane_h */ { ls_lua_pop(L, 2); return -1; } ret = ls_lua_pcall(L, 0, 1, 0); /* lanes ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } ref = ls_luaL_ref(L, LUA_REGISTRYINDEX); ls_lua_pop(L, 1); return ref; }
static LIST *ls_lua_callhelper(int top, int ret) { LIST *retList; int numParams; int i; if (ret != 0) { if (ls_lua_isstring(L, -1)) { printf("jam: Error compiling Lua code\n%s\n", ls_lua_tostring(L, -1)); exit(EXITBAD); } ls_lua_pop(L, 1); return L0; } ret = ls_lua_pcall(L, 0, -1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) { int ret; printf("jam: Error running Lua code\n%s\n", ls_lua_tostring(L, -1)); ls_lua_getglobal(L, "debug"); ls_lua_getfield(L, -1, "traceback"); ret = ls_lua_pcall(L, 0, 1, 0); if (ret == 0) { if (ls_lua_isstring(L, -1)) { puts(ls_lua_tostring(L, -1)); } } exit(EXITBAD); } ls_lua_pop(L, 1); return L0; } retList = L0; numParams = ls_lua_gettop(L) - top; for (i = 1; i <= numParams; ++i) { retList = luahelper_addtolist(L, retList, top + i); } return retList; }
int luahelper_md5callback(const char *filename, MD5SUM sum, const char* callback) { int ret; ls_lua_init(); ls_lua_getfield(L, LUA_GLOBALSINDEX, callback); if (!ls_lua_isfunction(L, -1)) { ls_lua_pop(L, 1); printf("jam: Error calling Lua md5 callback '%s'.\n", callback); memset(sum, 0, sizeof(MD5SUM)); return 0; } ls_lua_pushstring(L, filename); ret = ls_lua_pcall(L, 1, 1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error running Lua md5 callback\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 1); memset(sum, 0, sizeof(MD5SUM)); return 0; } if (ls_lua_isnil(L, -1)) { memset(sum, 0, sizeof(MD5SUM)); ls_lua_pop(L, 1); return 0; } if (!ls_lua_isstring(L, -1) || ls_lua_objlen(L, -1) != sizeof(MD5SUM)) { printf("jam: Error running Lua md5 callback '%s'.\n", callback); memset(sum, 0, sizeof(MD5SUM)); ls_lua_pop(L, 1); return 0; } memcpy(sum, ls_lua_tostring(L, -1), sizeof(MD5SUM)); ls_lua_pop(L, 1); return 1; }
int luahelper_push_linefilter(const char* actionName) { ls_lua_init(); ls_lua_getglobal(L, "LineFilters"); /* LineFilters */ ls_lua_getfield(L, -1, actionName); /* LineFilters function */ if (!ls_lua_isfunction(L, -1)) { ls_lua_pop(L, 2); return 0; } ls_lua_remove(L, -2); linefilter_stack_position = ls_lua_gettop(L); return 1; }
void luahelper_taskcancel(int taskid) { int ret; ls_lua_init(); ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid); ls_lua_pushvalue(L, -1); ls_lua_getfield(L, -1, "cancel"); ls_lua_pushvalue(L, -2); ls_lua_pushnumber(L, 0); ls_lua_pushboolean(L, 1); ret = ls_lua_pcall(L, 3, -1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error running Lua task.cancel\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return; } ls_lua_pop(L, 1); }
static int pmain (ls_lua_State *L) { int top; int ret; ls_luaL_openlibs(L); ls_lua_pushcclosure(L, LS_jam_getvar, 0); ls_lua_setglobal(L, "jam_getvar"); ls_lua_pushcclosure(L, LS_jam_setvar, 0); ls_lua_setglobal(L, "jam_setvar"); ls_lua_pushcclosure(L, LS_jam_action, 0); ls_lua_setglobal(L, "jam_action"); ls_lua_pushcclosure(L, LS_jam_evaluaterule, 0); ls_lua_setglobal(L, "jam_evaluaterule"); ls_lua_pushcclosure(L, LS_jam_expand, 0); ls_lua_setglobal(L, "jam_expand"); ls_lua_pushcclosure(L, LS_jam_parse, 0); ls_lua_setglobal(L, "jam_parse"); ls_lua_pushcclosure(L, LS_jam_print, 0); ls_lua_setglobal(L, "jam_print"); top = ls_lua_gettop(L); ret = ls_luaL_loadstring(L, "lanes = require 'lanes'"); ls_lua_callhelper(top, ret); ls_lua_getglobal(L, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "configure"); /* lanes configure */ ls_lua_newtable(L); /* lanes configure table */ ls_lua_pushcclosure(L, lanes_on_state_create, 0); /* lanes configure table lanes_on_state_create */ ls_lua_setfield(L, -2, "on_state_create"); /* lanes configure table */ ret = ls_lua_pcall(L, 1, 0, 0); /* lanes */ if (ret != 0) { const char* err = ls_lua_tostring(L, -1); (void)err; } ls_lua_pop(L, 2); ls_lua_newtable(L); ls_lua_setglobal(L, "LineFilters"); return 0; }
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; }
int luahelper_taskisrunning(int taskid, int* returnValue) { const char* status; ls_lua_init(); ls_lua_rawgeti(L, LUA_REGISTRYINDEX, taskid); /* lane_h */ if (!ls_lua_istable(L, -1)) { *returnValue = 1; ls_lua_pop(L, 1); return 0; } ls_lua_getfield(L, -1, "status"); /* lane_h status */ status = ls_lua_tostring(L, -1); if (strcmp(status, "done") == 0) { int ret; ls_lua_pop(L, 1); /* lane_h */ ls_lua_getfield(L, -1, "join"); /* lane_h join(function) */ ls_lua_pushvalue(L, -2); /* lane_h join(function) lane_h */ ret = ls_lua_pcall(L, 1, 1, 0); /* lane_h ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1)); *returnValue = 1; } if (ls_lua_isnumber(L, -1)) *returnValue = (int)ls_lua_tonumber(L, -1); ls_lua_pop(L, 2); ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid); return 0; } else if (strcmp(status, "error") == 0 || strcmp(status, "cancelled") == 0) { int ret; *returnValue = 1; ls_lua_pop(L, 1); /* lane_h */ ls_lua_getfield(L, -1, "join"); /* lane_h join(function) */ ls_lua_pushvalue(L, -2); /* lane_h join(function) lane_h */ ret = ls_lua_pcall(L, 1, 3, 0); /* lane_h nil err stack_tbl */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return 0; } if (ls_lua_isstring(L, -2)) { printf("jam: Error in Lua lane\n%s\n", ls_lua_tostring(L, -2)); } ls_lua_pop(L, 4); /* */ ls_luaL_unref(L, LUA_REGISTRYINDEX, taskid); return 0; } ls_lua_pop(L, 2); return 1; }
// 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; }
int luahelper_taskadd(const char* taskscript, LOL* args) { int ret; int ref; size_t taskscriptlen = strlen(taskscript); char* newTaskScript; int i; ls_lua_init(); ls_lua_getglobal(L, "lanes"); /* lanes */ ls_lua_getfield(L, -1, "gen"); /* lanes gen */ ls_lua_pushstring(L, "*"); /* lanes gen * */ newTaskScript = malloc( taskscriptlen + 1 ); strncpy(newTaskScript, taskscript, taskscriptlen); newTaskScript[taskscriptlen] = 0; ret = ls_luaL_loadstring(L, newTaskScript); /* lanes gen * script */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error compiling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); printf("%s\n", newTaskScript); free(newTaskScript); return -1; } free(newTaskScript); ret = ls_lua_pcall(L, 2, 1, 0); /* lanes lane_h */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error creating Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } if (!ls_lua_isfunction(L, -1)) /* lanes lane_h */ { ls_lua_pop(L, 2); return -1; } for (i = 0; i < args->count; ++i) { LIST* list = lol_get(args, i); LISTITEM *l2; int index = 0; ls_lua_newtable(L); for (l2 = list_first(list); l2; l2 = list_next(l2)) { ls_lua_pushstring(L, list_value(l2)); ls_lua_rawseti(L, -2, ++index); } } ret = ls_lua_pcall(L, args->count, 1, 0); /* lanes ret */ if (ret != 0) { if (ls_lua_isstring(L, -1)) printf("jam: Error calling Lua lane\n%s\n", ls_lua_tostring(L, -1)); ls_lua_pop(L, 2); return -1; } ref = ls_luaL_ref(L, LUA_REGISTRYINDEX); ls_lua_pop(L, 1); return ref; }