int LS_jam_setvar(ls_lua_State *L) { int numParams = ls_lua_gettop(L); if (numParams < 2 || numParams > 3) return 0; if (!ls_lua_isstring(L, 1)) return 0; if (numParams == 2) { var_set(ls_lua_tostring(L, 1), luahelper_addtolist(L, L0, 2), VAR_SET); } else { TARGET *t; if (!ls_lua_isstring(L, 2)) return 0; t = bindtarget(ls_lua_tostring(L, 1)); pushsettings(t->settings); var_set(ls_lua_tostring(L, 2), luahelper_addtolist(L, L0, 3), VAR_SET); popsettings(t->settings); } return 0; }
int LS_jam_expand(ls_lua_State *L) { LIST *list = L0; LISTITEM* item; int index; int numParams = ls_lua_gettop(L); if (numParams < 1 || numParams > 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; { LOL lol; const char* src = ls_lua_tostring(L, 1); lol_init(&lol); list = var_expand(L0, src, src + strlen(src), &lol, 0); } 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; }
int LS_jam_evaluaterule(ls_lua_State *L) { LOL lol; int i; LIST *list; int index; int numParams = ls_lua_gettop(L); if (numParams < 1) return 0; if (!ls_lua_isstring(L, 1)) return 0; lol_init(&lol); for (i = 0; i < numParams - 1; ++i) { lol_add(&lol, luahelper_addtolist(L, L0, 2 + i)); } list = evaluate_rule(ls_lua_tostring(L, 1), &lol, L0); lol_free(&lol); ls_lua_newtable(L); index = 1; for (; list; list = list_next(list), ++index) { ls_lua_pushnumber(L, index); ls_lua_pushstring(L, list->string); ls_lua_settable(L, -3); } return 1; }
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; }
LIST * builtin_luafile( PARSE *parse, LOL *args, int *jmp) { int top; int ret; LISTITEM *l2; int index = 0; LIST *l = lol_get(args, 0); if (!list_first(l)) { printf("jam: No argument passed to LuaFile\n"); exit(EXITBAD); } ls_lua_init(); top = ls_lua_gettop(L); ls_lua_newtable(L); for (l2 = list_first(lol_get(args, 1)); l2; l2 = list_next(l2)) { ls_lua_pushstring(L, list_value(l2)); ls_lua_rawseti(L, -2, ++index); } ls_lua_setglobal(L, "arg"); ret = ls_luaL_loadfile(L, list_value(list_first(l))); return ls_lua_callhelper(top, ret); }
const char* luahelper_linefilter(const char* line, size_t lineSize) { int ret; int top; char* out; if (linefilter_stack_position == -1) { fprintf(stderr, "jam: Line filter access not enabled.\n"); exit(1); } ls_lua_init(); top = ls_lua_gettop(L); ls_lua_pushvalue(L, linefilter_stack_position); ls_lua_pushlstring(L, line, lineSize); /* function line */ ret = ls_lua_pcall(L, 1, 1, 0); if (ret != 0) { if (ls_lua_isstring(L, -1)) fprintf(stderr, "jam: Error running line filter.\n%s\n", ls_lua_tostring(L, -1)); ls_lua_settop(L, top); return NULL; } out = malloc(ls_lua_rawlen(L, -1) + 1); strcpy(out, ls_lua_tostring(L, -1)); ls_lua_settop(L, top); return out; }
LIST * builtin_luafile( PARSE *parse, LOL *args, int *jmp) { int top; int ret; LIST *l2; int index = 0; LIST *l = lol_get(args, 0); if (!l) { printf("jam: No argument passed to LuaFile\n"); exit(EXITBAD); } ls_lua_init(); top = ls_lua_gettop(L); ls_lua_newtable(L); for (l2 = lol_get(args, 1); l2; l2 = l2->next) { ls_lua_pushstring(L, l2->string); ls_lua_rawseti(L, -2, ++index); } ls_lua_setfield(L, LUA_GLOBALSINDEX, "arg"); ret = ls_luaL_loadfile(L, l->string); return ls_lua_callhelper(top, ret); }
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; }
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 *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; }
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_setfield(L, LUA_GLOBALSINDEX, "jam_getvar"); ls_lua_pushcclosure(L, LS_jam_setvar, 0); ls_lua_setfield(L, LUA_GLOBALSINDEX, "jam_setvar"); ls_lua_pushcclosure(L, LS_jam_evaluaterule, 0); ls_lua_setfield(L, LUA_GLOBALSINDEX, "jam_evaluaterule"); top = ls_lua_gettop(L); ret = ls_luaL_loadstring(L, "require 'lanes'"); ls_lua_callhelper(top, ret); return 0; }
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; }
LIST * builtin_luastring( PARSE *parse, LOL *args, int *jmp) { int top; int ret; LIST *l = lol_get(args, 0); if (!l) { printf("jam: No argument passed to LuaString\n"); exit(EXITBAD); } ls_lua_init(); top = ls_lua_gettop(L); ret = ls_luaL_loadstring(L, l->string); return ls_lua_callhelper(top, ret); }
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; }
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; }
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; }