struct action* mk_action3(int type, int p1_type, int p2_type, int p3_type, void* p1, void* p2, void* p3) { struct action* a; a=mk_action(type, p1_type, p2_type, p1, p2); if (a) { a->p3_type=p3_type; a->p3.data=p3; } return a; }
void *cbUpdateAi(void *data) { move_t m; ai_share_t *as; game_t *gm; NOT(data); as = data; gm = as->game; ai_find_move(&m, gm->turn, gm, NULL, &as->loading); mk_action(gm, &m, &as->action); as->loading = 1.f; as->shareEnd = true; return NULL; }
void updateGameAreYouSureQuit(GUI *g, game_t *gm, Controls *c) { updateMenuWidget(&g->gameAreYouSureQuit, c); if (submitted(c)) { g->next = guiFocusGameMenu; if (g->gameAreYouSureQuit.focus == yes) { move_t m; action_t a; g->next = guiFocusGameOver; m.type = MOVE_QUIT; m.playerIdx = gm->turn; mk_action(gm, &m, &a); assert(a.type == ACTION_QUIT); apply_action(&a, gm); } return; } if (goBack(c)) g->next = guiFocusGameMenu; }
static int l_siplua_moduleFunc(lua_State *L) { struct sipapi_object *o; const char *func; int n, nargs; cmd_export_t *exp_func_struct; action_elem_t elems[MAX_ACTION_ELEMS]; const char *s, *msg; char *str; int i; struct action *act; int retval; o = luaL_checkudata(L, 1, "siplua.api"); func = luaL_checkstring(L, 2); n = lua_gettop(L); nargs = n - 2; if (n - 1 > MAX_ACTION_ELEMS) return luaL_error(L, "function '%s' called with too many arguments [%d > %d]", func, nargs, MAX_ACTION_ELEMS - 1); exp_func_struct = find_cmd_export_t((char *)func, nargs, 0); if (!exp_func_struct) { return luaL_error(L, "function '%s' called, but not available."); } elems[0].type = CMD_ST; elems[0].u.data = exp_func_struct; memset(&elems[1], '\0', nargs * sizeof(action_elem_t)); for (i = 0; i < nargs; ++i) { s = lua_tostring(L, 3 + i); if (!s) { siplua_moduleFunc_free(func, exp_func_struct, elems, nargs); msg = lua_pushfstring(L, "%s expected, got %s", lua_typename(L, LUA_TSTRING), luaL_typename(L, 3 + i)); return luaL_argerror(L, 3 + i, msg); } str = pkg_malloc(strlen(s) + 1); if (!str) { siplua_moduleFunc_free(func, exp_func_struct, elems, nargs); return luaL_error(L, "Not enough memory"); } strcpy(str, s); /* We should maybe try STR_ST and elems[].u.str.{s,len} */ elems[i + 1].type = STRING_ST; elems[i + 1].u.data = str; /* elems[].u.string */ } act = mk_action(MODULE_T, n - 2 + 1, elems, 0, "lua"); if (!act) { siplua_moduleFunc_free(func, exp_func_struct, elems, nargs); return luaL_error(L, "action structure could not be created. Error."); } /* siplua_log(L_DBG, "fixup/%p free_fixup/%p", */ /* exp_func_struct->fixup, */ /* exp_func_struct->free_fixup); */ if (exp_func_struct->fixup) { if (!siplua_unsafemodfnc && !exp_func_struct->free_fixup) { siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs); return luaL_error(L, "Module function '%s' is unsafe. Call is refused.\n", func); } if (nargs == 0) { retval = exp_func_struct->fixup(0, 0); if (retval < 0) { siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs); return luaL_error(L, "Error in fixup (0)\n"); } } for (i = 0; i < nargs; ++i) { retval = exp_func_struct->fixup(&act->elem[i + 1].u.data, i + 1); if (retval < 0) { siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs); return luaL_error(L, "Error in fixup (%d)\n", i + 1); } act->elem[i + 1].type = MODFIXUP_ST; } } retval = do_action(act, o->msg); siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs); pkg_free(act); lua_pushinteger(L, retval); return 1; }
int moduleFunc(struct sip_msg *m, char *func, char *param1, char *param2, int *retval) { cmd_export_t *exp_func_struct; struct action *act; char *argv[2]; int argc = 0; action_elem_t elems[MAX_ACTION_ELEMS]; if (!func) { LM_ERR("moduleFunc called with null function name. Error."); return -1; } if ((!param1) && param2) { LM_ERR("moduleFunc called with parameter 1 UNSET and" " parameter 2 SET. Error."); return -1; } if (param1) { argv[0] = (char *)pkg_malloc(strlen(param1)+1); strcpy(argv[0], param1); argc++; } else { argv[0] = NULL; } if (param2) { argv[1] = (char *)pkg_malloc(strlen(param2)+1); strcpy(argv[1], param2); argc++; } else { argv[1] = NULL; } exp_func_struct = find_cmd_export_t(func, argc, 0); if (!exp_func_struct) { LM_ERR("function '%s' called, but not available.", func); *retval = -1; if (argv[0]) pkg_free(argv[0]); if (argv[1]) pkg_free(argv[1]); return -1; } elems[0].type = CMD_ST; elems[0].u.data = exp_func_struct; elems[1].type = STRING_ST; elems[1].u.data = argv[0]; elems[2].type = STRING_ST; elems[2].u.data = argv[1]; act = mk_action( MODULE_T, 3, elems, 0); if (!act) { LM_ERR("action structure could not be created. Error."); if (argv[0]) pkg_free(argv[0]); if (argv[1]) pkg_free(argv[1]); return -1; } if (exp_func_struct->fixup) { if (!unsafemodfnc) { LM_ERR("Module function '%s' is unsafe. Call is refused.\n", func); if (argv[0]) pkg_free(argv[0]); if (argv[1]) pkg_free(argv[1]); *retval = -1; return -1; } if (argc>=2) { *retval = exp_func_struct->fixup(&(act->elem[2].u.data), 2); if (*retval < 0) { LM_ERR("Error in fixup (2)\n"); return -1; } act->elem[2].type = MODFIXUP_ST; } if (argc>=1) { *retval = exp_func_struct->fixup(&(act->elem[1].u.data), 1); if (*retval < 0) { LM_ERR("Error in fixup (1)\n"); return -1; } act->elem[1].type = MODFIXUP_ST; } if (argc==0) { *retval = exp_func_struct->fixup(0, 0); if (*retval < 0) { LM_ERR("Error in fixup (0)\n"); return -1; } } } *retval = do_action(act, m); if ((act->elem[2].type == MODFIXUP_ST) && (act->elem[2].u.data)) { /* pkg_free(act->elem[2].u.data); */ LM_WARN("moduleFunction: A fixup function was called. " "This currently creates a memory leak.\n"); } if ((act->elem[1].type == MODFIXUP_ST) && (act->elem[1].u.data)) { /* pkg_free(act->elem[1].u.data); */ LM_WARN("moduleFunction: A fixup function was called. " "This currently creates a memory leak.\n"); } if (argv[0]) pkg_free(argv[0]); if (argv[1]) pkg_free(argv[1]); pkg_free(act); return 1; }
static PyObject * msg_call_function(msgobject *self, PyObject *args) { /* TODO: adapt to new cmd_export and params interface */ #if 0 int i, rval; char *fname, *arg1, *arg2; cmd_export_t *fexport; struct action *act; action_elem_t elems[MAX_ACTION_ELEMS]; if (self->msg == NULL) { PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL"); Py_INCREF(Py_None); return Py_None; } i = PySequence_Size(args); if (i < 1 || i > 3) { PyErr_SetString(PyExc_RuntimeError, "call_function() should " \ "have from 1 to 3 arguments"); Py_INCREF(Py_None); return Py_None; } if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2)) return NULL; fexport = find_cmd_export_t(fname, 0); if (fexport == NULL) { PyErr_SetString(PyExc_RuntimeError, "no such function"); Py_INCREF(Py_None); return Py_None; } elems[0].type = CMD_ST; elems[0].u.data = fexport; elems[1].type = STRING_ST; elems[1].u.data = arg1; elems[2].type = STRING_ST; elems[2].u.data = arg2; act = mk_action(MODULE_T, 3, elems, 0, "python"); if (act == NULL) { PyErr_SetString(PyExc_RuntimeError, "action structure could not be created"); Py_INCREF(Py_None); return Py_None; } if (fexport->fixup != NULL) { if (i >= 3) { rval = fexport->fixup(&(act->elem[2].u.data), 2); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)"); Py_INCREF(Py_None); return Py_None; } act->elem[2].type = MODFIXUP_ST; } if (i >= 2) { rval = fexport->fixup(&(act->elem[1].u.data), 1); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)"); Py_INCREF(Py_None); return Py_None; } act->elem[1].type = MODFIXUP_ST; } if (i == 1) { rval = fexport->fixup(0, 0); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)"); Py_INCREF(Py_None); return Py_None; } } } rval = do_action(act, self->msg); if ((act->elem[2].type == MODFIXUP_ST) && (act->elem[2].u.data) && (act->elem[2].u.data != arg2)) { pkg_free(act->elem[2].u.data); } if ((act->elem[1].type == MODFIXUP_ST) && (act->elem[1].u.data)) { pkg_free(act->elem[1].u.data); } pkg_free(act); return PyInt_FromLong(rval); #endif return NULL; }
static PyObject *msg_call_function(msgobject *self, PyObject *args) { int i, rval; char *fname, *arg1, *arg2; sr31_cmd_export_t* fexport; struct action *act; struct run_act_ctx ra_ctx; unsigned mod_ver; if (self->msg == NULL) { PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL"); Py_INCREF(Py_None); return Py_None; } i = PySequence_Size(args); if (i < 1 || i > 3) { PyErr_SetString(PyExc_RuntimeError, "call_function() should " \ "have from 1 to 3 arguments"); Py_INCREF(Py_None); return Py_None; } if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2)) return NULL; fexport = find_export_record(fname, i - 1, 0, &mod_ver); if (fexport == NULL) { PyErr_SetString(PyExc_RuntimeError, "no such function"); Py_INCREF(Py_None); return Py_None; } act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */, MODEXP_ST, fexport, /* function */ NUMBER_ST, 2, /* parameter number */ STRING_ST, arg1, /* param. 1 */ STRING_ST, arg2 /* param. 2 */ ); if (act == NULL) { PyErr_SetString(PyExc_RuntimeError, "action structure could not be created"); Py_INCREF(Py_None); return Py_None; } if (fexport->fixup != NULL) { if (i >= 3) { rval = fexport->fixup(&(act->val[3].u.data), 2); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)"); Py_INCREF(Py_None); return Py_None; } act->val[3].type = MODFIXUP_ST; } if (i >= 2) { rval = fexport->fixup(&(act->val[2].u.data), 1); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)"); Py_INCREF(Py_None); return Py_None; } act->val[2].type = MODFIXUP_ST; } if (i == 1) { rval = fexport->fixup(0, 0); if (rval < 0) { PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)"); Py_INCREF(Py_None); return Py_None; } } } init_run_actions_ctx(&ra_ctx); rval = do_action(&ra_ctx, act, self->msg); if ((act->val[3].type == MODFIXUP_ST) && (act->val[3].u.data)) { pkg_free(act->val[3].u.data); } if ((act->val[2].type == MODFIXUP_ST) && (act->val[2].u.data)) { pkg_free(act->val[2].u.data); } pkg_free(act); return PyInt_FromLong(rval); }
static int lua_sr_modf (lua_State *L) { int ret; char *luav[MAX_ACTIONS]; char *argv[MAX_ACTIONS]; int argc; int i; int mod_type; struct run_act_ctx ra_ctx; unsigned modver; struct action *act; sr31_cmd_export_t* expf; sr_lua_env_t *env_L; ret = 1; act = NULL; argc = 0; memset(luav, 0, MAX_ACTIONS*sizeof(char*)); memset(argv, 0, MAX_ACTIONS*sizeof(char*)); env_L = sr_lua_env_get(); if(env_L->msg==NULL) goto error; #if 0 app_lua_dump_stack(L); #endif argc = lua_gettop(L); if(argc==0) { LM_ERR("name of module function not provided\n"); goto error; } if(argc>=MAX_ACTIONS) { LM_ERR("too many parameters\n"); goto error; } /* first is function name, then parameters */ for(i=1; i<=argc; i++) { if (!lua_isstring(L, i)) { LM_ERR("invalid parameter type (%d)\n", i); goto error; } luav[i-1] = (char*)lua_tostring(L, i); } /* pkg copy only parameters */ for(i=1; i<MAX_ACTIONS; i++) { if(luav[i]!=NULL) { argv[i] = (char*)pkg_malloc(strlen(luav[i])+1); if(argv[i]==NULL) { LM_ERR("no more pkg\n"); goto error; } strcpy(argv[i], luav[i]); } } expf = find_export_record(luav[0], argc-1, 0, &modver); if (expf==NULL) { LM_ERR("function '%s' is not available\n", luav[0]); goto error; } /* check fixups */ if (expf->fixup!=NULL && expf->free_fixup==NULL) { LM_ERR("function '%s' has fixup - cannot be used\n", luav[0]); goto error; } switch(expf->param_no) { case 0: mod_type = MODULE0_T; break; case 1: mod_type = MODULE1_T; break; case 2: mod_type = MODULE2_T; break; case 3: mod_type = MODULE3_T; break; case 4: mod_type = MODULE4_T; break; case 5: mod_type = MODULE5_T; break; case 6: mod_type = MODULE6_T; break; case VAR_PARAM_NO: mod_type = MODULEX_T; break; default: LM_ERR("unknown/bad definition for function '%s' (%d params)\n", luav[0], expf->param_no); goto error; } act = mk_action(mod_type, argc+1 /* number of (type, value) pairs */, MODEXP_ST, expf, /* function */ NUMBER_ST, argc-1, /* parameter number */ STRING_ST, argv[1], /* param. 1 */ STRING_ST, argv[2], /* param. 2 */ STRING_ST, argv[3], /* param. 3 */ STRING_ST, argv[4], /* param. 4 */ STRING_ST, argv[5], /* param. 5 */ STRING_ST, argv[6] /* param. 6 */ ); if (act==NULL) { LM_ERR("action structure could not be created for '%s'\n", luav[0]); goto error; } /* handle fixups */ if (expf->fixup) { if(argc==1) { /* no parameters */ if(expf->fixup(0, 0)<0) { LM_ERR("Error in fixup (0) for '%s'\n", luav[0]); goto error; } } else { for(i=1; i<argc; i++) { if(expf->fixup(&(act->val[i+1].u.data), i)<0) { LM_ERR("Error in fixup (%d) for '%s'\n", i, luav[0]); goto error; } act->val[i+1].type = MODFIXUP_ST; } } } init_run_actions_ctx(&ra_ctx); ret = do_action(&ra_ctx, act, env_L->msg); /* free fixups */ if (expf->fixup) { for(i=1; i<argc; i++) { if ((act->val[i+1].type == MODFIXUP_ST) && (act->val[i+1].u.data)) { expf->free_fixup(&(act->val[i+1].u.data), i); } } } pkg_free(act); for(i=0; i<MAX_ACTIONS; i++) { if(argv[i]!=NULL) pkg_free(argv[i]); argv[i] = 0; } lua_pushinteger(L, ret); return 1; error: if(act!=NULL) pkg_free(act); for(i=0; i<MAX_ACTIONS; i++) { if(argv[i]!=NULL) pkg_free(argv[i]); argv[i] = 0; } lua_pushinteger(L, -1); return 1; }
void updateGameGUI(GUI *g, Controls *c, game_t *gm) { Cmd cmd; move_t m; action_t a; Log l; GameGUI *gg; TransMove *tm; NOT(g); NOT(c); NOT(gm); gg = &g->gameGui; tm = &g->transMove; if (c->hardware.key[hardwareKeyStart].type == KEY_STATE_PRESSED) { g->next = guiFocusGameMenu; return; } if (tm->type == transMoveInvalid) { resetNewGameGui(g, gm); } switch (gg->focus) { case gameGUIFocusBoard: boardWidgetControls(&cmd, gg, c); break; case gameGUIFocusRack: rackWidgetControls(&cmd, gg, c); break; default: break; } updateGameGUIViaCmd(gg, &cmd, tm->type); switch (cmd.type) { case cmdFocusTop: { if (tm->type == transMovePlace || tm->type == transMovePlaceEnd || tm->type == transMovePlacePlay) { gg->focus = gameGUIFocusBoard; } break; } case cmdFocusBottom: { if (tm->type == transMovePlaceWild) { break; } gg->focus = gameGUIFocusRack; break; } case cmdBoard: { if (tm->type == transMoveDiscard || tm->type == transMoveDiscardPlay) { break; } gg->focus = gameGUIFocusBoard; gg->boardWidget.index = cmd.data.board; break; } case cmdRack: { if (tm->type == transMoveSkip) { break; } gg->focus = gameGUIFocusRack; gg->rackWidget.index.x = cmd.data.rack; gg->rackWidget.index.y = 0; break; } default: break; } if (cmd.type == cmdInvalid) { gg->lastInput += SPF; } else { gg->lastInput = 0; } /* printCmd(&c); */ if (updateTransMove(tm, &cmd, &gm->board, &gm->player[gm->turn])) { /* printTransMove(&e->transMove); */ updateBoardWidget(&gg->boardWidget, tm, &gm->board); updateRackWidget(&gg->rackWidget, tm); if (tm->type == transMoveDiscard) { gg->focus = gameGUIFocusRack; } } transMoveToMove(&m, tm); mk_action(gm, &m, &a); apply_action(&a, gm); if (cmd.type == cmdPlay || cmd.type == cmdQuit) mkLog(&a, &l); if (a.type != ACTION_INVALID) { addActionToTextLog(&gg->textLog, &a); actionToLastMove(&gg->lastMove, &a); apply_adjust(tm->adjust.data.tile, &gm->player[a.playerIdx]); /* log action */ if (end_game(gm)) { g->next = guiFocusGameOver; /* log game over */ } else { next_turn(gm); clrTransMove(tm, gm->turn, &gm->player[gm->turn], &gm->board); g->next = nextGUIFocusByplayer_tag_t(gm->player[gm->turn].type); } tm->type = transMoveInvalid; } else { if (m.type != MOVE_INVALID) { /* printActionErr(a.type); */ } } if (cmd.type == cmdPlay) { gg->validPlay = a.type != ACTION_INVALID ? yes : no; } else { gg->validPlay = yesNoInvalid; } updateScoreBoard(&g->scoreBoard, gm, SPF); }
static int sr_mono_modf(MonoString *nfunc) { int ret; int mod_type; struct run_act_ctx ra_ctx; struct action *act = NULL; ksr_cmd_export_t* expf; sr_mono_env_t *env_M; char *func = NULL; env_M = sr_mono_env_get(); if(env_M->msg==NULL) goto error; func = mono_string_to_utf8(nfunc); expf = find_export_record(func, 0, 0); if (expf==NULL) { LM_ERR("function '%s' is not available\n", func); goto error; } /* check fixups */ if (expf->fixup!=NULL && expf->free_fixup==NULL) { LM_ERR("function '%s' has fixup - cannot be used\n", func); goto error; } mod_type = MODULE0_T; act = mk_action(mod_type, 1 /* number of (type, value) pairs */, MODEXP_ST, expf, /* function */ NUMBER_ST, 0, /* parameter number */ STRING_ST, NULL, /* param. 1 */ STRING_ST, NULL, /* param. 2 */ STRING_ST, NULL, /* param. 3 */ STRING_ST, NULL, /* param. 4 */ STRING_ST, NULL, /* param. 5 */ STRING_ST, NULL /* param. 6 */ ); if (act==NULL) { LM_ERR("action structure could not be created for '%s'\n", func); goto error; } /* handle fixups */ if (expf->fixup) { /* no parameters */ if(expf->fixup(0, 0)<0) { LM_ERR("Error in fixup (0) for '%s'\n", func); goto error; } } init_run_actions_ctx(&ra_ctx); ret = do_action(&ra_ctx, act, env_M->msg); pkg_free(act); mono_free(func); return ret; error: if(func!=NULL) mono_free(func); if(act!=NULL) pkg_free(act); return -127; }