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 LS_jam_getvar(ls_lua_State *L) { LIST *list; LISTITEM* item; int index; int numParams = ls_lua_gettop(L); if (numParams < 1 || numParams > 2) return 0; if (!ls_lua_isstring(L, 1)) return 0; if (numParams == 1) { list = var_get(ls_lua_tostring(L, 1)); } else { TARGET *t; if (!ls_lua_isstring(L, 2)) return 0; t = bindtarget(ls_lua_tostring(L, 1)); pushsettings(t->settings); list = var_get(ls_lua_tostring(L, 2)); popsettings(t->settings); } ls_lua_newtable(L); index = 1; for (item = list_first(list); item; item = list_next(item), ++index) { ls_lua_pushnumber(L, index); ls_lua_pushstring(L, list_value(item)); ls_lua_settable(L, -3); } return 1; }
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)) { printf("jam: Error running Lua code\n%s\n", 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 LS_jam_print(ls_lua_State *L) { int numParams = ls_lua_gettop(L); if (numParams < 1 || numParams > 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; puts(ls_lua_tostring(L, 1)); 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 LS_jam_evaluaterule(ls_lua_State *L) { LOL lol; int i; LIST *list; LISTITEM* item; int index; const char* rule; int numParams = ls_lua_gettop(L); if (numParams < 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; lol_init(&lol); rule = ls_lua_tostring(L, 1); for (i = 0; i < numParams - 1; ++i) { lol_add(&lol, luahelper_addtolist(L, L0, 2 + i)); } list = evaluate_rule(rule, &lol, L0); lol_free(&lol); ls_lua_newtable(L); index = 1; for (item = list_first(list); item; item = list_next(item), ++index) { ls_lua_pushnumber(L, index); ls_lua_pushstring(L, list_value(item)); ls_lua_settable(L, -3); } 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); }
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; }
int LS_jam_parse(ls_lua_State *L) { int numParams = ls_lua_gettop(L); if (numParams < 1 || numParams > 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; { const char* src = ls_lua_tostring(L, 1); const char* ptr = src; const char* startPtr; char** lines; int numberOfLines = 1; int status; while (*ptr) { if (*ptr == '\n') { ++numberOfLines; } ++ptr; } lines = malloc(sizeof(char*) * (numberOfLines + 1)); numberOfLines = 0; startPtr = ptr = src; while (1) { if (*ptr == '\n' || *ptr == 0) { char* line; if (*ptr == '\n') ++ptr; line = malloc(ptr - startPtr + 1); memcpy(line, startPtr, ptr - startPtr); line[ptr - startPtr] = 0; startPtr = ptr; lines[numberOfLines++] = line; if (*ptr == 0) break; } else { ++ptr; } } lines[numberOfLines] = 0; parse_lines(lines[0], lines); status = yyanyerrors(); while (numberOfLines > 0) { free(lines[--numberOfLines]); } free(lines); if (status) { struct ls_lua_Debug ar; if (ls_lua_getstack(L, 1, &ar)) { ls_lua_getinfo(L, "nSl", &ar); } printf("jam: Error parsing Jam code near %s[%d].\n", ar.short_src, ar.currentline); exit(EXITBAD); } } return 0; }
// 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; }