/*! * \brief [lua_CFunction] Return a lua 'variable' object (for access from lua, don't call * directly) * * This function is called to lookup a variable construct a 'variable' object. * It would be called in the following example as would be seen in * extensions.lua. * * \code * channel.variable * \endcode */ static int lua_get_variable(lua_State *L) { struct ast_channel *chan; const char *name = luaL_checkstring(L, 2); char *value = NULL; char *workspace = alloca(LUA_BUF_SIZE); workspace[0] = '\0'; lua_getfield(L, LUA_REGISTRYINDEX, "channel"); chan = lua_touserdata(L, -1); lua_pop(L, 1); lua_pushvalue(L, 2); lua_push_variable_table(L); /* if this is not a request for a dialplan funciton attempt to retrieve * the value of the variable */ if (!ast_strlen_zero(name) && name[strlen(name) - 1] != ')') { pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan)); } if (value) { lua_pushstring(L, value); lua_setfield(L, -2, "value"); } return 1; }
/*! * \brief [lua_CFunction] Used to get the value of a variable or dialplan * function (for access from lua, don't call directly) * * The value of the variable or function is returned. This function is the * 'get()' function in the following example as would be seen in * extensions.lua. * * \code * channel.variable:get() * \endcode */ static int lua_get_variable_value(lua_State *L) { struct ast_channel *chan; char *value = NULL, *name; char *workspace = alloca(LUA_BUF_SIZE); int autoservice; workspace[0] = '\0'; if (!lua_istable(L, 1)) { lua_pushstring(L, "User probably used '.' instead of ':' for retrieving a channel variable value"); return lua_error(L); } lua_getfield(L, LUA_REGISTRYINDEX, "channel"); chan = lua_touserdata(L, -1); lua_pop(L, 1); lua_getfield(L, 1, "name"); name = ast_strdupa(lua_tostring(L, -1)); lua_pop(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); autoservice = lua_toboolean(L, -1); lua_pop(L, 1); if (autoservice) ast_autoservice_stop(chan); /* if this is a dialplan function then use ast_func_read(), otherwise * use pbx_retrieve_variable() */ if (!ast_strlen_zero(name) && name[strlen(name) - 1] == ')') { value = ast_func_read(chan, name, workspace, LUA_BUF_SIZE) ? NULL : workspace; } else { pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan)); } if (autoservice) ast_autoservice_start(chan); if (value) { lua_pushstring(L, value); } else { lua_pushnil(L); } return 1; }
static char *function_fieldqty(struct cw_channel *chan, int argc, char **argv, char *buf, size_t len) { char *varval, workspace[256]; int fieldcount = 0; if (argc != 2 || !argv[0][0] || !argv[1][0]) { cw_log(LOG_ERROR, "Syntax: %s\n", fieldqty_func_syntax); return NULL; } pbx_retrieve_variable(chan, argv[0], &varval, workspace, sizeof(workspace), NULL); while (strsep(&varval, argv[1])) fieldcount++; snprintf(buf, len, "%d", fieldcount); return buf; }