diff options
Diffstat (limited to 'main/pbx.c')
-rw-r--r-- | main/pbx.c | 2080 |
1 files changed, 3 insertions, 2077 deletions
diff --git a/main/pbx.c b/main/pbx.c index 7c58e613a..08ae5b6c3 100644 --- a/main/pbx.c +++ b/main/pbx.c @@ -322,24 +322,6 @@ struct ast_context { char name[0]; /*!< Name of the context */ }; -/*! \brief ast_app: A registered application */ -struct ast_app { - int (*execute)(struct ast_channel *chan, const char *data); - AST_DECLARE_STRING_FIELDS( - AST_STRING_FIELD(synopsis); /*!< Synopsis text for 'show applications' */ - AST_STRING_FIELD(description); /*!< Description (help text) for 'show application <name>' */ - AST_STRING_FIELD(syntax); /*!< Syntax text for 'core show applications' */ - AST_STRING_FIELD(arguments); /*!< Arguments description */ - AST_STRING_FIELD(seealso); /*!< See also */ - ); -#ifdef AST_XML_DOCS - enum ast_doc_src docsrc; /*!< Where the documentation come from. */ -#endif - AST_RWLIST_ENTRY(ast_app) list; /*!< Next app in list */ - struct ast_module *module; /*!< Module this app belongs to */ - char name[0]; /*!< Name of the application */ -}; - /*! \brief ast_state_cb: An extension state notify register item */ struct ast_state_cb { /*! Watcher ID returned when registered. */ @@ -731,10 +713,6 @@ static unsigned int hashtab_hash_labels(const void *obj) return ast_hashtab_hash_string(S_OR(ac->label, "")); } - -AST_RWLOCK_DEFINE_STATIC(globalslock); -static struct varshead globals = AST_LIST_HEAD_NOLOCK_INIT_VALUE; - static int autofallthrough = 1; static int extenpatternmatchnew = 0; static char *overrideswitch = NULL; @@ -764,15 +742,6 @@ AST_MUTEX_DEFINE_STATIC(conlock); */ AST_MUTEX_DEFINE_STATIC(context_merge_lock); -/*! - * \brief Registered applications container. - * - * It is sorted by application name. - */ -static AST_RWLIST_HEAD_STATIC(apps, ast_app); - -static AST_RWLIST_HEAD_STATIC(switches, ast_switch); - static int stateid = 1; /*! * \note When holding this container's lock, do _not_ do @@ -962,86 +931,6 @@ int check_contexts(char *file, int line ) } #endif -/* - \note This function is special. It saves the stack so that no matter - how many times it is called, it returns to the same place */ -int pbx_exec(struct ast_channel *c, /*!< Channel */ - struct ast_app *app, /*!< Application */ - const char *data) /*!< Data for execution */ -{ - int res; - struct ast_module_user *u = NULL; - const char *saved_c_appl; - const char *saved_c_data; - - /* save channel values */ - saved_c_appl= ast_channel_appl(c); - saved_c_data= ast_channel_data(c); - - ast_channel_lock(c); - ast_channel_appl_set(c, app->name); - ast_channel_data_set(c, data); - ast_channel_publish_snapshot(c); - ast_channel_unlock(c); - - if (app->module) - u = __ast_module_user_add(app->module, c); - res = app->execute(c, S_OR(data, "")); - if (app->module && u) - __ast_module_user_remove(app->module, u); - /* restore channel values */ - ast_channel_appl_set(c, saved_c_appl); - ast_channel_data_set(c, saved_c_data); - return res; -} - -static struct ast_app *pbx_findapp_nolock(const char *name) -{ - struct ast_app *cur; - int cmp; - - AST_RWLIST_TRAVERSE(&apps, cur, list) { - cmp = strcasecmp(name, cur->name); - if (cmp > 0) { - continue; - } - if (!cmp) { - /* Found it. */ - break; - } - /* Not in container. */ - cur = NULL; - break; - } - - return cur; -} - -struct ast_app *pbx_findapp(const char *app) -{ - struct ast_app *ret; - - AST_RWLIST_RDLOCK(&apps); - ret = pbx_findapp_nolock(app); - AST_RWLIST_UNLOCK(&apps); - - return ret; -} - -static struct ast_switch *pbx_findswitch(const char *sw) -{ - struct ast_switch *asw; - - AST_RWLIST_RDLOCK(&switches); - AST_RWLIST_TRAVERSE(&switches, asw, list) { - if (!strcasecmp(asw->name, sw)) - break; - } - AST_RWLIST_UNLOCK(&switches); - - return asw; -} - static inline int include_valid(struct ast_include *i) { if (!i->hastime) @@ -2783,291 +2672,6 @@ struct ast_exten *pbx_find_extension(struct ast_channel *chan, return NULL; } -/*! - * \brief extract offset:length from variable name. - * \return 1 if there is a offset:length part, which is - * trimmed off (values go into variables) - */ -static int parse_variable_name(char *var, int *offset, int *length, int *isfunc) -{ - int parens = 0; - - *offset = 0; - *length = INT_MAX; - *isfunc = 0; - for (; *var; var++) { - if (*var == '(') { - (*isfunc)++; - parens++; - } else if (*var == ')') { - parens--; - } else if (*var == ':' && parens == 0) { - *var++ = '\0'; - sscanf(var, "%30d:%30d", offset, length); - return 1; /* offset:length valid */ - } - } - return 0; -} - -/*! - *\brief takes a substring. It is ok to call with value == workspace. - * \param value - * \param offset < 0 means start from the end of the string and set the beginning - * to be that many characters back. - * \param length is the length of the substring, a value less than 0 means to leave - * that many off the end. - * \param workspace - * \param workspace_len - * Always return a copy in workspace. - */ -static char *substring(const char *value, int offset, int length, char *workspace, size_t workspace_len) -{ - char *ret = workspace; - int lr; /* length of the input string after the copy */ - - ast_copy_string(workspace, value, workspace_len); /* always make a copy */ - - lr = strlen(ret); /* compute length after copy, so we never go out of the workspace */ - - /* Quick check if no need to do anything */ - if (offset == 0 && length >= lr) /* take the whole string */ - return ret; - - if (offset < 0) { /* translate negative offset into positive ones */ - offset = lr + offset; - if (offset < 0) /* If the negative offset was greater than the length of the string, just start at the beginning */ - offset = 0; - } - - /* too large offset result in empty string so we know what to return */ - if (offset >= lr) - return ret + lr; /* the final '\0' */ - - ret += offset; /* move to the start position */ - if (length >= 0 && length < lr - offset) /* truncate if necessary */ - ret[length] = '\0'; - else if (length < 0) { - if (lr > offset - length) /* After we remove from the front and from the rear, is there anything left? */ - ret[lr + length - offset] = '\0'; - else - ret[0] = '\0'; - } - - return ret; -} - -static const char *ast_str_substring(struct ast_str *value, int offset, int length) -{ - int lr; /* length of the input string after the copy */ - - lr = ast_str_strlen(value); /* compute length after copy, so we never go out of the workspace */ - - /* Quick check if no need to do anything */ - if (offset == 0 && length >= lr) /* take the whole string */ - return ast_str_buffer(value); - - if (offset < 0) { /* translate negative offset into positive ones */ - offset = lr + offset; - if (offset < 0) /* If the negative offset was greater than the length of the string, just start at the beginning */ - offset = 0; - } - - /* too large offset result in empty string so we know what to return */ - if (offset >= lr) { - ast_str_reset(value); - return ast_str_buffer(value); - } - - if (offset > 0) { - /* Go ahead and chop off the beginning */ - memmove(ast_str_buffer(value), ast_str_buffer(value) + offset, ast_str_strlen(value) - offset + 1); - lr -= offset; - } - - if (length >= 0 && length < lr) { /* truncate if necessary */ - char *tmp = ast_str_buffer(value); - tmp[length] = '\0'; - ast_str_update(value); - } else if (length < 0) { - if (lr > -length) { /* After we remove from the front and from the rear, is there anything left? */ - char *tmp = ast_str_buffer(value); - tmp[lr + length] = '\0'; - ast_str_update(value); - } else { - ast_str_reset(value); - } - } else { - /* Nothing to do, but update the buffer length */ - ast_str_update(value); - } - - return ast_str_buffer(value); -} - -/*! \brief Support for Asterisk built-in variables in the dialplan - -\note See also - - \ref AstVar Channel variables - - \ref AstCauses The HANGUPCAUSE variable - */ -void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp) -{ - struct ast_str *str = ast_str_create(16); - const char *cret; - - cret = ast_str_retrieve_variable(&str, 0, c, headp, var); - ast_copy_string(workspace, ast_str_buffer(str), workspacelen); - *ret = cret ? workspace : NULL; - ast_free(str); -} - -const char *ast_str_retrieve_variable(struct ast_str **str, ssize_t maxlen, struct ast_channel *c, struct varshead *headp, const char *var) -{ - const char not_found = '\0'; - char *tmpvar; - const char *ret; - const char *s; /* the result */ - int offset, length; - int i, need_substring; - struct varshead *places[2] = { headp, &globals }; /* list of places where we may look */ - char workspace[20]; - - if (c) { - ast_channel_lock(c); - places[0] = ast_channel_varshead(c); - } - /* - * Make a copy of var because parse_variable_name() modifies the string. - * Then if called directly, we might need to run substring() on the result; - * remember this for later in 'need_substring', 'offset' and 'length' - */ - tmpvar = ast_strdupa(var); /* parse_variable_name modifies the string */ - need_substring = parse_variable_name(tmpvar, &offset, &length, &i /* ignored */); - - /* - * Look first into predefined variables, then into variable lists. - * Variable 's' points to the result, according to the following rules: - * s == ¬_found (set at the beginning) means that we did not find a - * matching variable and need to look into more places. - * If s != ¬_found, s is a valid result string as follows: - * s = NULL if the variable does not have a value; - * you typically do this when looking for an unset predefined variable. - * s = workspace if the result has been assembled there; - * typically done when the result is built e.g. with an snprintf(), - * so we don't need to do an additional copy. - * s != workspace in case we have a string, that needs to be copied - * (the ast_copy_string is done once for all at the end). - * Typically done when the result is already available in some string. - */ - s = ¬_found; /* default value */ - if (c) { /* This group requires a valid channel */ - /* Names with common parts are looked up a piece at a time using strncmp. */ - if (!strncmp(var, "CALL", 4)) { - if (!strncmp(var + 4, "ING", 3)) { - if (!strcmp(var + 7, "PRES")) { /* CALLINGPRES */ - ast_str_set(str, maxlen, "%d", - ast_party_id_presentation(&ast_channel_caller(c)->id)); - s = ast_str_buffer(*str); - } else if (!strcmp(var + 7, "ANI2")) { /* CALLINGANI2 */ - ast_str_set(str, maxlen, "%d", ast_channel_caller(c)->ani2); - s = ast_str_buffer(*str); - } else if (!strcmp(var + 7, "TON")) { /* CALLINGTON */ - ast_str_set(str, maxlen, "%d", ast_channel_caller(c)->id.number.plan); - s = ast_str_buffer(*str); - } else if (!strcmp(var + 7, "TNS")) { /* CALLINGTNS */ - ast_str_set(str, maxlen, "%d", ast_channel_dialed(c)->transit_network_select); - s = ast_str_buffer(*str); - } - } - } else if (!strcmp(var, "HINT")) { - s = ast_str_get_hint(str, maxlen, NULL, 0, c, ast_channel_context(c), ast_channel_exten(c)) ? ast_str_buffer(*str) : NULL; - } else if (!strcmp(var, "HINTNAME")) { - s = ast_str_get_hint(NULL, 0, str, maxlen, c, ast_channel_context(c), ast_channel_exten(c)) ? ast_str_buffer(*str) : NULL; - } else if (!strcmp(var, "EXTEN")) { - s = ast_channel_exten(c); - } else if (!strcmp(var, "CONTEXT")) { - s = ast_channel_context(c); - } else if (!strcmp(var, "PRIORITY")) { - ast_str_set(str, maxlen, "%d", ast_channel_priority(c)); - s = ast_str_buffer(*str); - } else if (!strcmp(var, "CHANNEL")) { - s = ast_channel_name(c); - } else if (!strcmp(var, "UNIQUEID")) { - s = ast_channel_uniqueid(c); - } else if (!strcmp(var, "HANGUPCAUSE")) { - ast_str_set(str, maxlen, "%d", ast_channel_hangupcause(c)); - s = ast_str_buffer(*str); - } - } - if (s == ¬_found) { /* look for more */ - if (!strcmp(var, "EPOCH")) { - ast_str_set(str, maxlen, "%d", (int) time(NULL)); - s = ast_str_buffer(*str); - } else if (!strcmp(var, "SYSTEMNAME")) { - s = ast_config_AST_SYSTEM_NAME; - } else if (!strcmp(var, "ASTETCDIR")) { - s = ast_config_AST_CONFIG_DIR; - } else if (!strcmp(var, "ASTMODDIR")) { - s = ast_config_AST_MODULE_DIR; - } else if (!strcmp(var, "ASTVARLIBDIR")) { - s = ast_config_AST_VAR_DIR; - } else if (!strcmp(var, "ASTDBDIR")) { - s = ast_config_AST_DB; - } else if (!strcmp(var, "ASTKEYDIR")) { - s = ast_config_AST_KEY_DIR; - } else if (!strcmp(var, "ASTDATADIR")) { - s = ast_config_AST_DATA_DIR; - } else if (!strcmp(var, "ASTAGIDIR")) { - s = ast_config_AST_AGI_DIR; - } else if (!strcmp(var, "ASTSPOOLDIR")) { - s = ast_config_AST_SPOOL_DIR; - } else if (!strcmp(var, "ASTRUNDIR")) { - s = ast_config_AST_RUN_DIR; - } else if (!strcmp(var, "ASTLOGDIR")) { - s = ast_config_AST_LOG_DIR; - } else if (!strcmp(var, "ENTITYID")) { - ast_eid_to_str(workspace, sizeof(workspace), &ast_eid_default); - s = workspace; - } - } - /* if not found, look into chanvars or global vars */ - for (i = 0; s == ¬_found && i < ARRAY_LEN(places); i++) { - struct ast_var_t *variables; - if (!places[i]) - continue; - if (places[i] == &globals) - ast_rwlock_rdlock(&globalslock); - AST_LIST_TRAVERSE(places[i], variables, entries) { - if (!strcmp(ast_var_name(variables), var)) { - s = ast_var_value(variables); - break; - } - } - if (places[i] == &globals) - ast_rwlock_unlock(&globalslock); - } - if (s == ¬_found || s == NULL) { - ast_debug(5, "Result of '%s' is NULL\n", var); - ret = NULL; - } else { - ast_debug(5, "Result of '%s' is '%s'\n", var, s); - if (s != ast_str_buffer(*str)) { - ast_str_set(str, maxlen, "%s", s); - } - ret = ast_str_buffer(*str); - if (need_substring) { - ret = ast_str_substring(*str, offset, length); - ast_debug(2, "Final result of '%s' is '%s'\n", var, ret); - } - } - - if (c) { - ast_channel_unlock(c); - } - return ret; -} - static void exception_store_free(void *data) { struct pbx_exception *exception = data; @@ -3142,407 +2746,6 @@ static struct ast_custom_function exception_function = { .read = acf_exception_read, }; -void ast_str_substitute_variables_full(struct ast_str **buf, ssize_t maxlen, struct ast_channel *c, struct varshead *headp, const char *templ, size_t *used) -{ - /* Substitutes variables into buf, based on string templ */ - char *cp4 = NULL; - const char *whereweare; - int orig_size = 0; - int offset, offset2, isfunction; - const char *nextvar, *nextexp, *nextthing; - const char *vars, *vare; - char *finalvars; - int pos, brackets, needsub, len; - struct ast_str *substr1 = ast_str_create(16), *substr2 = NULL, *substr3 = ast_str_create(16); - - ast_str_reset(*buf); - whereweare = templ; - while (!ast_strlen_zero(whereweare)) { - /* reset our buffer */ - ast_str_reset(substr3); - - /* Assume we're copying the whole remaining string */ - pos = strlen(whereweare); - nextvar = NULL; - nextexp = NULL; - nextthing = strchr(whereweare, '$'); - if (nextthing) { - switch (nextthing[1]) { - case '{': - nextvar = nextthing; - pos = nextvar - whereweare; - break; - case '[': - nextexp = nextthing; - pos = nextexp - whereweare; - break; - default: - pos = 1; - } - } - - if (pos) { - /* Copy that many bytes */ - ast_str_append_substr(buf, maxlen, whereweare, pos); - - templ += pos; - whereweare += pos; - } - - if (nextvar) { - /* We have a variable. Find the start and end, and determine - if we are going to have to recursively call ourselves on the - contents */ - vars = vare = nextvar + 2; - brackets = 1; - needsub = 0; - - /* Find the end of it */ - while (brackets && *vare) { - if ((vare[0] == '$') && (vare[1] == '{')) { - needsub++; - } else if (vare[0] == '{') { - brackets++; - } else if (vare[0] == '}') { - brackets--; - } else if ((vare[0] == '$') && (vare[1] == '[')) - needsub++; - vare++; - } - if (brackets) - ast_log(LOG_WARNING, "Error in extension logic (missing '}')\n"); - len = vare - vars - 1; - - /* Skip totally over variable string */ - whereweare += (len + 3); - - /* Store variable name (and truncate) */ - ast_str_set_substr(&substr1, 0, vars, len); - ast_debug(5, "Evaluating '%s' (from '%s' len %d)\n", ast_str_buffer(substr1), vars, len); - - /* Substitute if necessary */ - if (needsub) { - size_t my_used; - - if (!substr2) { - substr2 = ast_str_create(16); - } - ast_str_substitute_variables_full(&substr2, 0, c, headp, ast_str_buffer(substr1), &my_used); - finalvars = ast_str_buffer(substr2); - } else { - finalvars = ast_str_buffer(substr1); - } - - parse_variable_name(finalvars, &offset, &offset2, &isfunction); - if (isfunction) { - /* Evaluate function */ - if (c || !headp) { - cp4 = ast_func_read2(c, finalvars, &substr3, 0) ? NULL : ast_str_buffer(substr3); - } else { - struct varshead old; - struct ast_channel *bogus = ast_dummy_channel_alloc(); - if (bogus) { - memcpy(&old, ast_channel_varshead(bogus), sizeof(old)); - memcpy(ast_channel_varshead(bogus), headp, sizeof(*ast_channel_varshead(bogus))); - cp4 = ast_func_read2(c, finalvars, &substr3, 0) ? NULL : ast_str_buffer(substr3); - /* Don't deallocate the varshead that was passed in */ - memcpy(ast_channel_varshead(bogus), &old, sizeof(*ast_channel_varshead(bogus))); - ast_channel_unref(bogus); - } else { - ast_log(LOG_ERROR, "Unable to allocate bogus channel for variable substitution. Function results may be blank.\n"); - } - } - ast_debug(2, "Function %s result is '%s'\n", finalvars, cp4 ? cp4 : "(null)"); - } else { - /* Retrieve variable value */ - ast_str_retrieve_variable(&substr3, 0, c, headp, finalvars); - cp4 = ast_str_buffer(substr3); - } - if (cp4) { - ast_str_substring(substr3, offset, offset2); - ast_str_append(buf, maxlen, "%s", ast_str_buffer(substr3)); - } - } else if (nextexp) { - /* We have an expression. Find the start and end, and determine - if we are going to have to recursively call ourselves on the - contents */ - vars = vare = nextexp + 2; - brackets = 1; - needsub = 0; - - /* Find the end of it */ - while (brackets && *vare) { - if ((vare[0] == '$') && (vare[1] == '[')) { - needsub++; - brackets++; - vare++; - } else if (vare[0] == '[') { - brackets++; - } else if (vare[0] == ']') { - brackets--; - } else if ((vare[0] == '$') && (vare[1] == '{')) { - needsub++; - vare++; - } - vare++; - } - if (brackets) - ast_log(LOG_WARNING, "Error in extension logic (missing ']')\n"); - len = vare - vars - 1; - - /* Skip totally over expression */ - whereweare += (len + 3); - - /* Store variable name (and truncate) */ - ast_str_set_substr(&substr1, 0, vars, len); - - /* Substitute if necessary */ - if (needsub) { - size_t my_used; - - if (!substr2) { - substr2 = ast_str_create(16); - } - ast_str_substitute_variables_full(&substr2, 0, c, headp, ast_str_buffer(substr1), &my_used); - finalvars = ast_str_buffer(substr2); - } else { - finalvars = ast_str_buffer(substr1); - } - - if (ast_str_expr(&substr3, 0, c, finalvars)) { - ast_debug(2, "Expression result is '%s'\n", ast_str_buffer(substr3)); - } - ast_str_append(buf, maxlen, "%s", ast_str_buffer(substr3)); - } - } - *used = ast_str_strlen(*buf) - orig_size; - ast_free(substr1); - ast_free(substr2); - ast_free(substr3); -} - -void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ) -{ - size_t used; - ast_str_substitute_variables_full(buf, maxlen, chan, NULL, templ, &used); -} - -void ast_str_substitute_variables_varshead(struct ast_str **buf, ssize_t maxlen, struct varshead *headp, const char *templ) -{ - size_t used; - ast_str_substitute_variables_full(buf, maxlen, NULL, headp, templ, &used); -} - -void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count, size_t *used) -{ - /* Substitutes variables into cp2, based on string cp1, cp2 NO LONGER NEEDS TO BE ZEROED OUT!!!! */ - char *cp4 = NULL; - const char *whereweare, *orig_cp2 = cp2; - int length, offset, offset2, isfunction; - char *workspace = NULL; - char *ltmp = NULL, *var = NULL; - char *nextvar, *nextexp, *nextthing; - char *vars, *vare; - int pos, brackets, needsub, len; - - *cp2 = 0; /* just in case nothing ends up there */ - whereweare = cp1; - while (!ast_strlen_zero(whereweare) && count) { - /* Assume we're copying the whole remaining string */ - pos = strlen(whereweare); - nextvar = NULL; - nextexp = NULL; - nextthing = strchr(whereweare, '$'); - if (nextthing) { - switch (nextthing[1]) { - case '{': - nextvar = nextthing; - pos = nextvar - whereweare; - break; - case '[': - nextexp = nextthing; - pos = nextexp - whereweare; - break; - default: - pos = 1; - } - } - - if (pos) { - /* Can't copy more than 'count' bytes */ - if (pos > count) - pos = count; - - /* Copy that many bytes */ - memcpy(cp2, whereweare, pos); - - count -= pos; - cp2 += pos; - whereweare += pos; - *cp2 = 0; - } - - if (nextvar) { - /* We have a variable. Find the start and end, and determine - if we are going to have to recursively call ourselves on the - contents */ - vars = vare = nextvar + 2; - brackets = 1; - needsub = 0; - - /* Find the end of it */ - while (brackets && *vare) { - if ((vare[0] == '$') && (vare[1] == '{')) { - needsub++; - } else if (vare[0] == '{') { - brackets++; - } else if (vare[0] == '}') { - brackets--; - } else if ((vare[0] == '$') && (vare[1] == '[')) - needsub++; - vare++; - } - if (brackets) - ast_log(LOG_WARNING, "Error in extension logic (missing '}')\n"); - len = vare - vars - 1; - - /* Skip totally over variable string */ - whereweare += (len + 3); - - if (!var) - var = ast_alloca(VAR_BUF_SIZE); - - /* Store variable name (and truncate) */ - ast_copy_string(var, vars, len + 1); - - /* Substitute if necessary */ - if (needsub) { - size_t my_used; - - if (!ltmp) { - ltmp = ast_alloca(VAR_BUF_SIZE); - } - pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1, &my_used); - vars = ltmp; - } else { - vars = var; - } - - if (!workspace) - workspace = ast_alloca(VAR_BUF_SIZE); - - workspace[0] = '\0'; - - parse_variable_name(vars, &offset, &offset2, &isfunction); - if (isfunction) { - /* Evaluate function */ - if (c || !headp) - cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace; - else { - struct varshead old; - struct ast_channel *c = ast_dummy_channel_alloc(); - if (c) { - memcpy(&old, ast_channel_varshead(c), sizeof(old)); - memcpy(ast_channel_varshead(c), headp, sizeof(*ast_channel_varshead(c))); - cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace; - /* Don't deallocate the varshead that was passed in */ - memcpy(ast_channel_varshead(c), &old, sizeof(*ast_channel_varshead(c))); - c = ast_channel_unref(c); - } else { - ast_log(LOG_ERROR, "Unable to allocate bogus channel for variable substitution. Function results may be blank.\n"); - } - } - ast_debug(2, "Function %s result is '%s'\n", vars, cp4 ? cp4 : "(null)"); - } else { - /* Retrieve variable value */ - pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp); - } - if (cp4) { - cp4 = substring(cp4, offset, offset2, workspace, VAR_BUF_SIZE); - - length = strlen(cp4); - if (length > count) - length = count; - memcpy(cp2, cp4, length); - count -= length; - cp2 += length; - *cp2 = 0; - } - } else if (nextexp) { - /* We have an expression. Find the start and end, and determine - if we are going to have to recursively call ourselves on the - contents */ - vars = vare = nextexp + 2; - brackets = 1; - needsub = 0; - - /* Find the end of it */ - while (brackets && *vare) { - if ((vare[0] == '$') && (vare[1] == '[')) { - needsub++; - brackets++; - vare++; - } else if (vare[0] == '[') { - brackets++; - } else if (vare[0] == ']') { - brackets--; - } else if ((vare[0] == '$') && (vare[1] == '{')) { - needsub++; - vare++; - } - vare++; - } - if (brackets) - ast_log(LOG_WARNING, "Error in extension logic (missing ']')\n"); - len = vare - vars - 1; - - /* Skip totally over expression */ - whereweare += (len + 3); - - if (!var) - var = ast_alloca(VAR_BUF_SIZE); - - /* Store variable name (and truncate) */ - ast_copy_string(var, vars, len + 1); - - /* Substitute if necessary */ - if (needsub) { - size_t my_used; - - if (!ltmp) { - ltmp = ast_alloca(VAR_BUF_SIZE); - } - pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1, &my_used); - vars = ltmp; - } else { - vars = var; - } - - length = ast_expr(vars, cp2, count, c); - - if (length) { - ast_debug(1, "Expression result is '%s'\n", cp2); - count -= length; - cp2 += length; - *cp2 = 0; - } - } - } - *used = cp2 - orig_cp2; -} - -void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count) -{ - size_t used; - pbx_substitute_variables_helper_full(c, (c) ? ast_channel_varshead(c) : NULL, cp1, cp2, count, &used); -} - -void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count) -{ - size_t used; - pbx_substitute_variables_helper_full(NULL, headp, cp1, cp2, count, &used); -} - /*! * \brief The return value depends on the action: * @@ -3617,11 +2820,11 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con, if (substitute) { pbx_substitute_variables_helper(c, substitute, passdata, sizeof(passdata)-1); } - ast_debug(1, "Launching '%s'\n", app->name); + ast_debug(1, "Launching '%s'\n", app_name(app)); if (VERBOSITY_ATLEAST(3)) { ast_verb(3, "Executing [%s@%s:%d] " COLORIZE_FMT "(\"" COLORIZE_FMT "\", \"" COLORIZE_FMT "\") %s\n", exten, context, priority, - COLORIZE(COLOR_BRCYAN, 0, app->name), + COLORIZE(COLOR_BRCYAN, 0, app_name(app)), COLORIZE(COLOR_BRMAGENTA, 0, ast_channel_name(c)), COLORIZE(COLOR_BRMAGENTA, 0, passdata), "in new stack"); @@ -4857,248 +4060,6 @@ void ast_pbx_h_exten_run(struct ast_channel *chan, const char *context) ast_channel_unlock(chan); } -/*! - * \internal - * \brief Publish a hangup handler related message to \ref stasis - */ -static void publish_hangup_handler_message(const char *action, struct ast_channel *chan, const char *handler) -{ - RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref); - - blob = ast_json_pack("{s: s, s: s}", - "type", action, - "handler", S_OR(handler, "")); - if (!blob) { - return; - } - - ast_channel_publish_blob(chan, ast_channel_hangup_handler_type(), blob); -} - -int ast_pbx_hangup_handler_run(struct ast_channel *chan) -{ - struct ast_hangup_handler_list *handlers; - struct ast_hangup_handler *h_handler; - - ast_channel_lock(chan); - handlers = ast_channel_hangup_handlers(chan); - if (AST_LIST_EMPTY(handlers)) { - ast_channel_unlock(chan); - return 0; - } - - /* - * Make sure that the channel is marked as hungup since we are - * going to run the hangup handlers on it. - */ - ast_softhangup_nolock(chan, AST_SOFTHANGUP_HANGUP_EXEC); - - for (;;) { - handlers = ast_channel_hangup_handlers(chan); - h_handler = AST_LIST_REMOVE_HEAD(handlers, node); - if (!h_handler) { - break; - } - - publish_hangup_handler_message("run", chan, h_handler->args); - ast_channel_unlock(chan); - - ast_app_exec_sub(NULL, chan, h_handler->args, 1); - ast_free(h_handler); - - ast_channel_lock(chan); - } - ast_channel_unlock(chan); - return 1; -} - -void ast_pbx_hangup_handler_init(struct ast_channel *chan) -{ - struct ast_hangup_handler_list *handlers; - - handlers = ast_channel_hangup_handlers(chan); - AST_LIST_HEAD_INIT_NOLOCK(handlers); -} - -void ast_pbx_hangup_handler_destroy(struct ast_channel *chan) -{ - struct ast_hangup_handler_list *handlers; - struct ast_hangup_handler *h_handler; - - ast_channel_lock(chan); - - /* Get rid of each of the hangup handlers on the channel */ - handlers = ast_channel_hangup_handlers(chan); - while ((h_handler = AST_LIST_REMOVE_HEAD(handlers, node))) { - ast_free(h_handler); - } - - ast_channel_unlock(chan); -} - -int ast_pbx_hangup_handler_pop(struct ast_channel *chan) -{ - struct ast_hangup_handler_list *handlers; - struct ast_hangup_handler *h_handler; - - ast_channel_lock(chan); - handlers = ast_channel_hangup_handlers(chan); - h_handler = AST_LIST_REMOVE_HEAD(handlers, node); - if (h_handler) { - publish_hangup_handler_message("pop", chan, h_handler->args); - } - ast_channel_unlock(chan); - if (h_handler) { - ast_free(h_handler); - return 1; - } - return 0; -} - -void ast_pbx_hangup_handler_push(struct ast_channel *chan, const char *handler) -{ - struct ast_hangup_handler_list *handlers; - struct ast_hangup_handler *h_handler; - const char *expanded_handler; - - if (ast_strlen_zero(handler)) { - return; - } - - expanded_handler = ast_app_expand_sub_args(chan, handler); - if (!expanded_handler) { - return; - } - h_handler = ast_malloc(sizeof(*h_handler) + 1 + strlen(expanded_handler)); - if (!h_handler) { - ast_free((char *) expanded_handler); - return; - } - strcpy(h_handler->args, expanded_handler);/* Safe */ - ast_free((char *) expanded_handler); - - ast_channel_lock(chan); - - handlers = ast_channel_hangup_handlers(chan); - AST_LIST_INSERT_HEAD(handlers, h_handler, node); - publish_hangup_handler_message("push", chan, h_handler->args); - ast_channel_unlock(chan); -} - -#define HANDLER_FORMAT "%-30s %s\n" - -/*! - * \internal - * \brief CLI output the hangup handler headers. - * \since 11.0 - * - * \param fd CLI file descriptor to use. - * - * \return Nothing - */ -static void ast_pbx_hangup_handler_headers(int fd) -{ - ast_cli(fd, HANDLER_FORMAT, "Channel", "Handler"); -} - -/*! - * \internal - * \brief CLI output the channel hangup handlers. - * \since 11.0 - * - * \param fd CLI file descriptor to use. - * \param chan Channel to show hangup handlers. - * - * \return Nothing - */ -static void ast_pbx_hangup_handler_show(int fd, struct ast_channel *chan) -{ - struct ast_hangup_handler_list *handlers; - struct ast_hangup_handler *h_handler; - int first = 1; - - ast_channel_lock(chan); - handlers = ast_channel_hangup_handlers(chan); - AST_LIST_TRAVERSE(handlers, h_handler, node) { - ast_cli(fd, HANDLER_FORMAT, first ? ast_channel_name(chan) : "", h_handler->args); - first = 0; - } - ast_channel_unlock(chan); -} - -/* - * \brief 'show hanguphandlers <channel>' CLI command implementation function... - */ -static char *handle_show_hangup_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_channel *chan; - - switch (cmd) { - case CLI_INIT: - e->command = "core show hanguphandlers"; - e->usage = - "Usage: core show hanguphandlers <channel>\n" - " Show hangup handlers of a specified channel.\n"; - return NULL; - case CLI_GENERATE: - return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args); - } - - if (a->argc < 4) { - return CLI_SHOWUSAGE; - } - - chan = ast_channel_get_by_name(a->argv[3]); - if (!chan) { - ast_cli(a->fd, "Channel does not exist.\n"); - return CLI_FAILURE; - } - - ast_pbx_hangup_handler_headers(a->fd); - ast_pbx_hangup_handler_show(a->fd, chan); - - ast_channel_unref(chan); - - return CLI_SUCCESS; -} - -/* - * \brief 'show hanguphandlers all' CLI command implementation function... - */ -static char *handle_show_hangup_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_channel_iterator *iter; - struct ast_channel *chan; - - switch (cmd) { - case CLI_INIT: - e->command = "core show hanguphandlers all"; - e->usage = - "Usage: core show hanguphandlers all\n" - " Show hangup handlers for all channels.\n"; - return NULL; - case CLI_GENERATE: - return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args); - } - - if (a->argc < 4) { - return CLI_SHOWUSAGE; - } - - iter = ast_channel_iterator_all_new(); - if (!iter) { - return CLI_FAILURE; - } - - ast_pbx_hangup_handler_headers(a->fd); - for (; (chan = ast_channel_iterator_next(iter)); ast_channel_unref(chan)) { - ast_pbx_hangup_handler_show(a->fd, chan); - } - ast_channel_iterator_destroy(iter); - - return CLI_SUCCESS; -} - /*! helper function to set extension and priority */ void set_ext_pri(struct ast_channel *c, const char *exten, int pri) { @@ -6007,247 +4968,10 @@ int ast_context_unlockmacro(const char *context) return ret; } -/*! \brief Dynamically register a new dial plan application */ -int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, const char *), const char *synopsis, const char *description, void *mod) -{ - struct ast_app *tmp; - struct ast_app *cur; - int length; -#ifdef AST_XML_DOCS - char *tmpxml; -#endif - - AST_RWLIST_WRLOCK(&apps); - cur = pbx_findapp_nolock(app); - if (cur) { - ast_log(LOG_WARNING, "Already have an application '%s'\n", app); - AST_RWLIST_UNLOCK(&apps); - return -1; - } - - length = sizeof(*tmp) + strlen(app) + 1; - - if (!(tmp = ast_calloc(1, length))) { - AST_RWLIST_UNLOCK(&apps); - return -1; - } - - if (ast_string_field_init(tmp, 128)) { - AST_RWLIST_UNLOCK(&apps); - ast_free(tmp); - return -1; - } - - strcpy(tmp->name, app); - tmp->execute = execute; - tmp->module = mod; - -#ifdef AST_XML_DOCS - /* Try to lookup the docs in our XML documentation database */ - if (ast_strlen_zero(synopsis) && ast_strlen_zero(description)) { - /* load synopsis */ - tmpxml = ast_xmldoc_build_synopsis("application", app, ast_module_name(tmp->module)); - ast_string_field_set(tmp, synopsis, tmpxml); - ast_free(tmpxml); - - /* load description */ - tmpxml = ast_xmldoc_build_description("application", app, ast_module_name(tmp->module)); - ast_string_field_set(tmp, description, tmpxml); - ast_free(tmpxml); - - /* load syntax */ - tmpxml = ast_xmldoc_build_syntax("application", app, ast_module_name(tmp->module)); - ast_string_field_set(tmp, syntax, tmpxml); - ast_free(tmpxml); - - /* load arguments */ - tmpxml = ast_xmldoc_build_arguments("application", app, ast_module_name(tmp->module)); - ast_string_field_set(tmp, arguments, tmpxml); - ast_free(tmpxml); - - /* load seealso */ - tmpxml = ast_xmldoc_build_seealso("application", app, ast_module_name(tmp->module)); - ast_string_field_set(tmp, seealso, tmpxml); - ast_free(tmpxml); - tmp->docsrc = AST_XML_DOC; - } else { -#endif - ast_string_field_set(tmp, synopsis, synopsis); - ast_string_field_set(tmp, description, description); -#ifdef AST_XML_DOCS - tmp->docsrc = AST_STATIC_DOC; - } -#endif - - /* Store in alphabetical order */ - AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) { - if (strcasecmp(tmp->name, cur->name) < 0) { - AST_RWLIST_INSERT_BEFORE_CURRENT(tmp, list); - break; - } - } - AST_RWLIST_TRAVERSE_SAFE_END; - if (!cur) - AST_RWLIST_INSERT_TAIL(&apps, tmp, list); - - ast_verb(2, "Registered application '" COLORIZE_FMT "'\n", COLORIZE(COLOR_BRCYAN, 0, tmp->name)); - - AST_RWLIST_UNLOCK(&apps); - - return 0; -} - -/* - * Append to the list. We don't have a tail pointer because we need - * to scan the list anyways to check for duplicates during insertion. - */ -int ast_register_switch(struct ast_switch *sw) -{ - struct ast_switch *tmp; - - AST_RWLIST_WRLOCK(&switches); - AST_RWLIST_TRAVERSE(&switches, tmp, list) { - if (!strcasecmp(tmp->name, sw->name)) { - AST_RWLIST_UNLOCK(&switches); - ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name); - return -1; - } - } - AST_RWLIST_INSERT_TAIL(&switches, sw, list); - AST_RWLIST_UNLOCK(&switches); - - return 0; -} - -void ast_unregister_switch(struct ast_switch *sw) -{ - AST_RWLIST_WRLOCK(&switches); - AST_RWLIST_REMOVE(&switches, sw, list); - AST_RWLIST_UNLOCK(&switches); -} - /* * Help for CLI commands ... */ -static void print_app_docs(struct ast_app *aa, int fd) -{ -#ifdef AST_XML_DOCS - char *synopsis = NULL, *description = NULL, *arguments = NULL, *seealso = NULL; - if (aa->docsrc == AST_XML_DOC) { - synopsis = ast_xmldoc_printable(S_OR(aa->synopsis, "Not available"), 1); - description = ast_xmldoc_printable(S_OR(aa->description, "Not available"), 1); - arguments = ast_xmldoc_printable(S_OR(aa->arguments, "Not available"), 1); - seealso = ast_xmldoc_printable(S_OR(aa->seealso, "Not available"), 1); - if (!synopsis || !description || !arguments || !seealso) { - goto free_docs; - } - ast_cli(fd, "\n" - "%s -= Info about application '%s' =- %s\n\n" - COLORIZE_FMT "\n" - "%s\n\n" - COLORIZE_FMT "\n" - "%s\n\n" - COLORIZE_FMT "\n" - "%s%s%s\n\n" - COLORIZE_FMT "\n" - "%s\n\n" - COLORIZE_FMT "\n" - "%s\n", - ast_term_color(COLOR_MAGENTA, 0), aa->name, ast_term_reset(), - COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"), synopsis, - COLORIZE(COLOR_MAGENTA, 0, "[Description]"), description, - COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"), - ast_term_color(COLOR_CYAN, 0), S_OR(aa->syntax, "Not available"), ast_term_reset(), - COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"), arguments, - COLORIZE(COLOR_MAGENTA, 0, "[See Also]"), seealso); -free_docs: - ast_free(synopsis); - ast_free(description); - ast_free(arguments); - ast_free(seealso); - } else -#endif - { - ast_cli(fd, "\n" - "%s -= Info about application '%s' =- %s\n\n" - COLORIZE_FMT "\n" - COLORIZE_FMT "\n\n" - COLORIZE_FMT "\n" - COLORIZE_FMT "\n\n" - COLORIZE_FMT "\n" - COLORIZE_FMT "\n\n" - COLORIZE_FMT "\n" - COLORIZE_FMT "\n\n" - COLORIZE_FMT "\n" - COLORIZE_FMT "\n", - ast_term_color(COLOR_MAGENTA, 0), aa->name, ast_term_reset(), - COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"), - COLORIZE(COLOR_CYAN, 0, S_OR(aa->synopsis, "Not available")), - COLORIZE(COLOR_MAGENTA, 0, "[Description]"), - COLORIZE(COLOR_CYAN, 0, S_OR(aa->description, "Not available")), - COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"), - COLORIZE(COLOR_CYAN, 0, S_OR(aa->syntax, "Not available")), - COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"), - COLORIZE(COLOR_CYAN, 0, S_OR(aa->arguments, "Not available")), - COLORIZE(COLOR_MAGENTA, 0, "[See Also]"), - COLORIZE(COLOR_CYAN, 0, S_OR(aa->seealso, "Not available"))); - } -} - -/* - * \brief 'show application' CLI command implementation function... - */ -static char *handle_show_application(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_app *aa; - int app, no_registered_app = 1; - - switch (cmd) { - case CLI_INIT: - e->command = "core show application"; - e->usage = - "Usage: core show application <application> [<application> [<application> [...]]]\n" - " Describes a particular application.\n"; - return NULL; - case CLI_GENERATE: - /* - * There is a possibility to show informations about more than one - * application at one time. You can type 'show application Dial Echo' and - * you will see informations about these two applications ... - */ - return ast_complete_applications(a->line, a->word, a->n); - } - - if (a->argc < 4) { - return CLI_SHOWUSAGE; - } - - AST_RWLIST_RDLOCK(&apps); - AST_RWLIST_TRAVERSE(&apps, aa, list) { - /* Check for each app that was supplied as an argument */ - for (app = 3; app < a->argc; app++) { - if (strcasecmp(aa->name, a->argv[app])) { - continue; - } - - /* We found it! */ - no_registered_app = 0; - - print_app_docs(aa, a->fd); - } - } - AST_RWLIST_UNLOCK(&apps); - - /* we found at least one app? no? */ - if (no_registered_app) { - ast_cli(a->fd, "Your application(s) is (are) not registered\n"); - return CLI_FAILURE; - } - - return CLI_SUCCESS; -} - /*! \brief handle_show_hints: CLI support for listing registered dial plan hints */ static char *handle_show_hints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { @@ -6413,40 +5137,6 @@ static char *handle_show_hint(struct ast_cli_entry *e, int cmd, struct ast_cli_a return CLI_SUCCESS; } - -/*! \brief handle_show_switches: CLI support for listing registered dial plan switches */ -static char *handle_show_switches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_switch *sw; - - switch (cmd) { - case CLI_INIT: - e->command = "core show switches"; - e->usage = - "Usage: core show switches\n" - " List registered switches\n"; - return NULL; - case CLI_GENERATE: - return NULL; - } - - AST_RWLIST_RDLOCK(&switches); - - if (AST_RWLIST_EMPTY(&switches)) { - AST_RWLIST_UNLOCK(&switches); - ast_cli(a->fd, "There are no registered alternative switches\n"); - return CLI_SUCCESS; - } - - ast_cli(a->fd, "\n -= Registered Asterisk Alternative Switches =-\n"); - AST_RWLIST_TRAVERSE(&switches, sw, list) - ast_cli(a->fd, "%s: %s\n", sw->name, sw->description); - - AST_RWLIST_UNLOCK(&switches); - - return CLI_SUCCESS; -} - #if 0 /* This code can be used to test if the system survives running out of memory. * It might be an idea to put this in only if ENABLE_AUTODESTRUCT_TESTS is enabled. @@ -6522,89 +5212,6 @@ static char *handle_eat_memory(struct ast_cli_entry *e, int cmd, struct ast_cli_ } #endif -static char *handle_show_applications(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_app *aa; - int like = 0, describing = 0; - int total_match = 0; /* Number of matches in like clause */ - int total_apps = 0; /* Number of apps registered */ - static const char * const choices[] = { "like", "describing", NULL }; - - switch (cmd) { - case CLI_INIT: - e->command = "core show applications [like|describing]"; - e->usage = - "Usage: core show applications [{like|describing} <text>]\n" - " List applications which are currently available.\n" - " If 'like', <text> will be a substring of the app name\n" - " If 'describing', <text> will be a substring of the description\n"; - return NULL; - case CLI_GENERATE: - return (a->pos != 3) ? NULL : ast_cli_complete(a->word, choices, a->n); - } - - AST_RWLIST_RDLOCK(&apps); - - if (AST_RWLIST_EMPTY(&apps)) { - ast_cli(a->fd, "There are no registered applications\n"); - AST_RWLIST_UNLOCK(&apps); - return CLI_SUCCESS; - } - - /* core list applications like <keyword> */ - if ((a->argc == 5) && (!strcmp(a->argv[3], "like"))) { - like = 1; - } else if ((a->argc > 4) && (!strcmp(a->argv[3], "describing"))) { - describing = 1; - } - - /* core list applications describing <keyword1> [<keyword2>] [...] */ - if ((!like) && (!describing)) { - ast_cli(a->fd, " -= Registered Asterisk Applications =-\n"); - } else { - ast_cli(a->fd, " -= Matching Asterisk Applications =-\n"); - } - - AST_RWLIST_TRAVERSE(&apps, aa, list) { - int printapp = 0; - total_apps++; - if (like) { - if (strcasestr(aa->name, a->argv[4])) { - printapp = 1; - total_match++; - } - } else if (describing) { - if (aa->description) { - /* Match all words on command line */ - int i; - printapp = 1; - for (i = 4; i < a->argc; i++) { - if (!strcasestr(aa->description, a->argv[i])) { - printapp = 0; - } else { - total_match++; - } - } - } - } else { - printapp = 1; - } - - if (printapp) { - ast_cli(a->fd," %20s: %s\n", aa->name, aa->synopsis ? aa->synopsis : "<Synopsis not available>"); - } - } - if ((!like) && (!describing)) { - ast_cli(a->fd, " -= %d Applications Registered =-\n",total_apps); - } else { - ast_cli(a->fd, " -= %d Applications Matching =-\n",total_match); - } - - AST_RWLIST_UNLOCK(&apps); - - return CLI_SUCCESS; -} - /* * 'show dialplan' CLI command implementation functions ... */ @@ -7195,34 +5802,6 @@ static int manager_show_dialplan(struct mansession *s, const struct message *m) return 0; } -/*! \brief CLI support for listing global variables in a parseable way */ -static char *handle_show_globals(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - int i = 0; - struct ast_var_t *newvariable; - - switch (cmd) { - case CLI_INIT: - e->command = "dialplan show globals"; - e->usage = - "Usage: dialplan show globals\n" - " List current global dialplan variables and their values\n"; - return NULL; - case CLI_GENERATE: - return NULL; - } - - ast_rwlock_rdlock(&globalslock); - AST_LIST_TRAVERSE (&globals, newvariable, entries) { - i++; - ast_cli(a->fd, " %s=%s\n", ast_var_name(newvariable), ast_var_value(newvariable)); - } - ast_rwlock_unlock(&globalslock); - ast_cli(a->fd, "\n -- %d variable(s)\n", i); - - return CLI_SUCCESS; -} - #ifdef AST_DEVMODE static char *handle_show_device2extenstate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { @@ -7253,102 +5832,6 @@ static char *handle_show_device2extenstate(struct ast_cli_entry *e, int cmd, str } #endif -/*! \brief CLI support for listing chanvar's variables in a parseable way */ -static char *handle_show_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_channel *chan; - struct ast_var_t *var; - - switch (cmd) { - case CLI_INIT: - e->command = "dialplan show chanvar"; - e->usage = - "Usage: dialplan show chanvar <channel>\n" - " List current channel variables and their values\n"; - return NULL; - case CLI_GENERATE: - return ast_complete_channels(a->line, a->word, a->pos, a->n, 3); - } - - if (a->argc != e->args + 1) { - return CLI_SHOWUSAGE; - } - - chan = ast_channel_get_by_name(a->argv[e->args]); - if (!chan) { - ast_cli(a->fd, "Channel '%s' not found\n", a->argv[e->args]); - return CLI_FAILURE; - } - - ast_channel_lock(chan); - AST_LIST_TRAVERSE(ast_channel_varshead(chan), var, entries) { - ast_cli(a->fd, "%s=%s\n", ast_var_name(var), ast_var_value(var)); - } - ast_channel_unlock(chan); - - ast_channel_unref(chan); - return CLI_SUCCESS; -} - -static char *handle_set_global(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - switch (cmd) { - case CLI_INIT: - e->command = "dialplan set global"; - e->usage = - "Usage: dialplan set global <name> <value>\n" - " Set global dialplan variable <name> to <value>\n"; - return NULL; - case CLI_GENERATE: - return NULL; - } - - if (a->argc != e->args + 2) - return CLI_SHOWUSAGE; - - pbx_builtin_setvar_helper(NULL, a->argv[3], a->argv[4]); - ast_cli(a->fd, "\n -- Global variable '%s' set to '%s'\n", a->argv[3], a->argv[4]); - - return CLI_SUCCESS; -} - -static char *handle_set_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -{ - struct ast_channel *chan; - const char *chan_name, *var_name, *var_value; - - switch (cmd) { - case CLI_INIT: - e->command = "dialplan set chanvar"; - e->usage = - "Usage: dialplan set chanvar <channel> <varname> <value>\n" - " Set channel variable <varname> to <value>\n"; - return NULL; - case CLI_GENERATE: - return ast_complete_channels(a->line, a->word, a->pos, a->n, 3); - } - - if (a->argc != e->args + 3) - return CLI_SHOWUSAGE; - - chan_name = a->argv[e->args]; - var_name = a->argv[e->args + 1]; - var_value = a->argv[e->args + 2]; - - if (!(chan = ast_channel_get_by_name(chan_name))) { - ast_cli(a->fd, "Channel '%s' not found\n", chan_name); - return CLI_FAILURE; - } - - pbx_builtin_setvar_helper(chan, var_name, var_value); - - chan = ast_channel_unref(chan); - - ast_cli(a->fd, "\n -- Channel variable '%s' set to '%s' for '%s'\n", var_name, var_value, chan_name); - - return CLI_SUCCESS; -} - static char *handle_set_extenpatternmatchnew(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { int oldval = 0; @@ -7412,27 +5895,18 @@ static struct ast_cli_entry pbx_cli[] = { #if 0 AST_CLI_DEFINE(handle_eat_memory, "Eats all available memory"), #endif - AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"), - AST_CLI_DEFINE(handle_show_switches, "Show alternative switches"), AST_CLI_DEFINE(handle_show_hints, "Show dialplan hints"), AST_CLI_DEFINE(handle_show_hint, "Show dialplan hint"), - AST_CLI_DEFINE(handle_show_globals, "Show global dialplan variables"), #ifdef AST_DEVMODE AST_CLI_DEFINE(handle_show_device2extenstate, "Show expected exten state from multiple device states"), #endif - AST_CLI_DEFINE(handle_show_chanvar, "Show channel variables"), - AST_CLI_DEFINE(handle_show_hangup_all, "Show hangup handlers of all channels"), - AST_CLI_DEFINE(handle_show_hangup_channel, "Show hangup handlers of a specified channel"), - AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"), - AST_CLI_DEFINE(handle_set_global, "Set global dialplan variable"), - AST_CLI_DEFINE(handle_set_chanvar, "Set a channel variable"), AST_CLI_DEFINE(handle_show_dialplan, "Show dialplan"), AST_CLI_DEFINE(handle_debug_dialplan, "Show fast extension pattern matching data structures"), AST_CLI_DEFINE(handle_unset_extenpatternmatchnew, "Use the Old extension pattern matching algorithm."), AST_CLI_DEFINE(handle_set_extenpatternmatchnew, "Use the New extension pattern matching algorithm."), }; -static void unreference_cached_app(struct ast_app *app) +void unreference_cached_app(struct ast_app *app) { struct ast_context *context = NULL; struct ast_exten *eroot = NULL, *e = NULL; @@ -7451,36 +5925,6 @@ static void unreference_cached_app(struct ast_app *app) return; } -int ast_unregister_application(const char *app) -{ - struct ast_app *cur; - int cmp; - - AST_RWLIST_WRLOCK(&apps); - AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) { - cmp = strcasecmp(app, cur->name); - if (cmp > 0) { - continue; - } - if (!cmp) { - /* Found it. */ - unreference_cached_app(cur); - AST_RWLIST_REMOVE_CURRENT(list); - ast_verb(2, "Unregistered application '%s'\n", cur->name); - ast_string_field_free_memory(cur); - ast_free(cur); - break; - } - /* Not in container. */ - cur = NULL; - break; - } - AST_RWLIST_TRAVERSE_SAFE_END; - AST_RWLIST_UNLOCK(&apps); - - return cur ? 0 : -1; -} - struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar) { struct ast_context *tmp, **local_contexts; @@ -7935,260 +6379,6 @@ int ast_context_add_include(const char *context, const char *include, const char return ret; } -/*! \brief Helper for get_range. - * return the index of the matching entry, starting from 1. - * If names is not supplied, try numeric values. - */ -static int lookup_name(const char *s, const char * const names[], int max) -{ - int i; - - if (names && *s > '9') { - for (i = 0; names[i]; i++) { - if (!strcasecmp(s, names[i])) { - return i; - } - } - } - - /* Allow months and weekdays to be specified as numbers, as well */ - if (sscanf(s, "%2d", &i) == 1 && i >= 1 && i <= max) { - /* What the array offset would have been: "1" would be at offset 0 */ - return i - 1; - } - return -1; /* error return */ -} - -/*! \brief helper function to return a range up to max (7, 12, 31 respectively). - * names, if supplied, is an array of names that should be mapped to numbers. - */ -static unsigned get_range(char *src, int max, const char * const names[], const char *msg) -{ - int start, end; /* start and ending position */ - unsigned int mask = 0; - char *part; - - /* Check for whole range */ - if (ast_strlen_zero(src) || !strcmp(src, "*")) { - return (1 << max) - 1; - } - - while ((part = strsep(&src, "&"))) { - /* Get start and ending position */ - char *endpart = strchr(part, '-'); - if (endpart) { - *endpart++ = '\0'; - } - /* Find the start */ - if ((start = lookup_name(part, names, max)) < 0) { - ast_log(LOG_WARNING, "Invalid %s '%s', skipping element\n", msg, part); - continue; - } - if (endpart) { /* find end of range */ - if ((end = lookup_name(endpart, names, max)) < 0) { - ast_log(LOG_WARNING, "Invalid end %s '%s', skipping element\n", msg, endpart); - continue; - } - } else { - end = start; - } - /* Fill the mask. Remember that ranges are cyclic */ - mask |= (1 << end); /* initialize with last element */ - while (start != end) { - mask |= (1 << start); - if (++start >= max) { - start = 0; - } - } - } - return mask; -} - -/*! \brief store a bitmask of valid times, one bit each 1 minute */ -static void get_timerange(struct ast_timing *i, char *times) -{ - char *endpart, *part; - int x; - int st_h, st_m; - int endh, endm; - int minute_start, minute_end; - - /* start disabling all times, fill the fields with 0's, as they may contain garbage */ - memset(i->minmask, 0, sizeof(i->minmask)); - - /* 1-minute per bit */ - /* Star is all times */ - if (ast_strlen_zero(times) || !strcmp(times, "*")) { - /* 48, because each hour takes 2 integers; 30 bits each */ - for (x = 0; x < 48; x++) { - i->minmask[x] = 0x3fffffff; /* 30 bits */ - } - return; - } - /* Otherwise expect a range */ - while ((part = strsep(×, "&"))) { - if (!(endpart = strchr(part, '-'))) { - if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) { - ast_log(LOG_WARNING, "%s isn't a valid time.\n", part); - continue; - } - i->minmask[st_h * 2 + (st_m >= 30 ? 1 : 0)] |= (1 << (st_m % 30)); - continue; - } - *endpart++ = '\0'; - /* why skip non digits? Mostly to skip spaces */ - while (*endpart && !isdigit(*endpart)) { - endpart++; - } - if (!*endpart) { - ast_log(LOG_WARNING, "Invalid time range starting with '%s-'.\n", part); - continue; - } - if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) { - ast_log(LOG_WARNING, "'%s' isn't a valid start time.\n", part); - continue; - } - if (sscanf(endpart, "%2d:%2d", &endh, &endm) != 2 || endh < 0 || endh > 23 || endm < 0 || endm > 59) { - ast_log(LOG_WARNING, "'%s' isn't a valid end time.\n", endpart); - continue; - } - minute_start = st_h * 60 + st_m; - minute_end = endh * 60 + endm; - /* Go through the time and enable each appropriate bit */ - for (x = minute_start; x != minute_end; x = (x + 1) % (24 * 60)) { - i->minmask[x / 30] |= (1 << (x % 30)); - } - /* Do the last one */ - i->minmask[x / 30] |= (1 << (x % 30)); - } - /* All done */ - return; -} - -static const char * const days[] = -{ - "sun", - "mon", - "tue", - "wed", - "thu", - "fri", - "sat", - NULL, -}; - -static const char * const months[] = -{ - "jan", - "feb", - "mar", - "apr", - "may", - "jun", - "jul", - "aug", - "sep", - "oct", - "nov", - "dec", - NULL, -}; -/*! /brief Build timing - * - * /param i info - * /param info_in - * - */ -int ast_build_timing(struct ast_timing *i, const char *info_in) -{ - char *info; - int j, num_fields, last_sep = -1; - - i->timezone = NULL; - - /* Check for empty just in case */ - if (ast_strlen_zero(info_in)) { - return 0; - } - - /* make a copy just in case we were passed a static string */ - info = ast_strdupa(info_in); - - /* count the number of fields in the timespec */ - for (j = 0, num_fields = 1; info[j] != '\0'; j++) { - if (info[j] == ',') { - last_sep = j; - num_fields++; - } - } - - /* save the timezone, if it is specified */ - if (num_fields == 5) { - i->timezone = ast_strdup(info + last_sep + 1); - } - - /* Assume everything except time */ - i->monthmask = 0xfff; /* 12 bits */ - i->daymask = 0x7fffffffU; /* 31 bits */ - i->dowmask = 0x7f; /* 7 bits */ - /* on each call, use strsep() to move info to the next argument */ - get_timerange(i, strsep(&info, "|,")); - if (info) - i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week"); - if (info) - i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day"); - if (info) - i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month"); - return 1; -} - -int ast_check_timing(const struct ast_timing *i) -{ - return ast_check_timing2(i, ast_tvnow()); -} - -int ast_check_timing2(const struct ast_timing *i, const struct timeval tv) -{ - struct ast_tm tm; - - ast_localtime(&tv, &tm, i->timezone); - - /* If it's not the right month, return */ - if (!(i->monthmask & (1 << tm.tm_mon))) - return 0; - - /* If it's not that time of the month.... */ - /* Warning, tm_mday has range 1..31! */ - if (!(i->daymask & (1 << (tm.tm_mday-1)))) - return 0; - - /* If it's not the right day of the week */ - if (!(i->dowmask & (1 << tm.tm_wday))) - return 0; - - /* Sanity check the hour just to be safe */ - if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) { - ast_log(LOG_WARNING, "Insane time...\n"); - return 0; - } - - /* Now the tough part, we calculate if it fits - in the right time based on min/hour */ - if (!(i->minmask[tm.tm_hour * 2 + (tm.tm_min >= 30 ? 1 : 0)] & (1 << (tm.tm_min >= 30 ? tm.tm_min - 30 : tm.tm_min)))) - return 0; - - /* If we got this far, then we're good */ - return 1; -} - -int ast_destroy_timing(struct ast_timing *i) -{ - if (i->timezone) { - ast_free(i->timezone); - i->timezone = NULL; - } - return 0; -} /* * errno values * ENOMEM - out of memory @@ -9739,238 +7929,6 @@ static struct ast_custom_function testtime_function = { .write = testtime_write, }; -int pbx_builtin_serialize_variables(struct ast_channel *chan, struct ast_str **buf) -{ - struct ast_var_t *variables; - const char *var, *val; - int total = 0; - - if (!chan) - return 0; - - ast_str_reset(*buf); - - ast_channel_lock(chan); - - AST_LIST_TRAVERSE(ast_channel_varshead(chan), variables, entries) { - if ((var = ast_var_name(variables)) && (val = ast_var_value(variables)) - /* && !ast_strlen_zero(var) && !ast_strlen_zero(val) */ - ) { - if (ast_str_append(buf, 0, "%s=%s\n", var, val) < 0) { - ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n"); - break; - } else - total++; - } else - break; - } - - ast_channel_unlock(chan); - - return total; -} - -const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name) -{ - struct ast_var_t *variables; - const char *ret = NULL; - int i; - struct varshead *places[2] = { NULL, &globals }; - - if (!name) - return NULL; - - if (chan) { - ast_channel_lock(chan); - places[0] = ast_channel_varshead(chan); - } - - for (i = 0; i < 2; i++) { - if (!places[i]) - continue; - if (places[i] == &globals) - ast_rwlock_rdlock(&globalslock); - AST_LIST_TRAVERSE(places[i], variables, entries) { - if (!strcmp(name, ast_var_name(variables))) { - ret = ast_var_value(variables); - break; - } - } - if (places[i] == &globals) - ast_rwlock_unlock(&globalslock); - if (ret) - break; - } - - if (chan) - ast_channel_unlock(chan); - - return ret; -} - -void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value) -{ - struct ast_var_t *newvariable; - struct varshead *headp; - - if (name[strlen(name)-1] == ')') { - char *function = ast_strdupa(name); - - ast_log(LOG_WARNING, "Cannot push a value onto a function\n"); - ast_func_write(chan, function, value); - return; - } - - if (chan) { - ast_channel_lock(chan); - headp = ast_channel_varshead(chan); - } else { - ast_rwlock_wrlock(&globalslock); - headp = &globals; - } - - if (value && (newvariable = ast_var_assign(name, value))) { - if (headp == &globals) - ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value); - AST_LIST_INSERT_HEAD(headp, newvariable, entries); - } - - if (chan) - ast_channel_unlock(chan); - else - ast_rwlock_unlock(&globalslock); -} - -int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value) -{ - struct ast_var_t *newvariable; - struct varshead *headp; - const char *nametail = name; - /*! True if the old value was not an empty string. */ - int old_value_existed = 0; - - if (name[strlen(name) - 1] == ')') { - char *function = ast_strdupa(name); - - return ast_func_write(chan, function, value); - } - - if (chan) { - ast_channel_lock(chan); - headp = ast_channel_varshead(chan); - } else { - ast_rwlock_wrlock(&globalslock); - headp = &globals; - } - - /* For comparison purposes, we have to strip leading underscores */ - if (*nametail == '_') { - nametail++; - if (*nametail == '_') - nametail++; - } - - AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) { - if (strcmp(ast_var_name(newvariable), nametail) == 0) { - /* there is already such a variable, delete it */ - AST_LIST_REMOVE_CURRENT(entries); - old_value_existed = !ast_strlen_zero(ast_var_value(newvariable)); - ast_var_delete(newvariable); - break; - } - } - AST_LIST_TRAVERSE_SAFE_END; - - if (value && (newvariable = ast_var_assign(name, value))) { - if (headp == &globals) { - ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value); - } - AST_LIST_INSERT_HEAD(headp, newvariable, entries); - ast_channel_publish_varset(chan, name, value); - } else if (old_value_existed) { - /* We just deleted a non-empty dialplan variable. */ - ast_channel_publish_varset(chan, name, ""); - } - - if (chan) - ast_channel_unlock(chan); - else - ast_rwlock_unlock(&globalslock); - return 0; -} - -int pbx_builtin_setvar(struct ast_channel *chan, const char *data) -{ - char *name, *value, *mydata; - - if (ast_strlen_zero(data)) { - ast_log(LOG_WARNING, "Set requires one variable name/value pair.\n"); - return 0; - } - - mydata = ast_strdupa(data); - name = strsep(&mydata, "="); - value = mydata; - if (!value) { - ast_log(LOG_WARNING, "Set requires an '=' to be a valid assignment.\n"); - return 0; - } - - if (strchr(name, ' ')) { - ast_log(LOG_WARNING, "Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n", name, mydata); - } - - pbx_builtin_setvar_helper(chan, name, value); - - return 0; -} - -int pbx_builtin_setvar_multiple(struct ast_channel *chan, const char *vdata) -{ - char *data; - int x; - AST_DECLARE_APP_ARGS(args, - AST_APP_ARG(pair)[24]; - ); - AST_DECLARE_APP_ARGS(pair, - AST_APP_ARG(name); - AST_APP_ARG(value); - ); - - if (ast_strlen_zero(vdata)) { - ast_log(LOG_WARNING, "MSet requires at least one variable name/value pair.\n"); - return 0; - } - - data = ast_strdupa(vdata); - AST_STANDARD_APP_ARGS(args, data); - - for (x = 0; x < args.argc; x++) { - AST_NONSTANDARD_APP_ARGS(pair, args.pair[x], '='); - if (pair.argc == 2) { - pbx_builtin_setvar_helper(chan, pair.name, pair.value); - if (strchr(pair.name, ' ')) - ast_log(LOG_WARNING, "Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n", pair.name, pair.value); - } else if (!chan) { - ast_log(LOG_WARNING, "MSet: ignoring entry '%s' with no '='\n", pair.name); - } else { - ast_log(LOG_WARNING, "MSet: ignoring entry '%s' with no '=' (in %s@%s:%d\n", pair.name, ast_channel_exten(chan), ast_channel_context(chan), ast_channel_priority(chan)); - } - } - - return 0; -} - -void pbx_builtin_clear_globals(void) -{ - struct ast_var_t *vardata; - - ast_rwlock_wrlock(&globalslock); - while ((vardata = AST_LIST_REMOVE_HEAD(&globals, entries))) - ast_var_delete(vardata); - ast_rwlock_unlock(&globalslock); -} - int pbx_checkcondition(const char *condition) { int res; @@ -10568,37 +8526,6 @@ int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string) return pbx_parseable_goto(chan, goto_string, 1); } -char *ast_complete_applications(const char *line, const char *word, int state) -{ - struct ast_app *app; - int which = 0; - int cmp; - char *ret = NULL; - size_t wordlen = strlen(word); - - AST_RWLIST_RDLOCK(&apps); - AST_RWLIST_TRAVERSE(&apps, app, list) { - cmp = strncasecmp(word, app->name, wordlen); - if (cmp > 0) { - continue; - } - if (!cmp) { - /* Found match. */ - if (++which <= state) { - /* Not enough matches. */ - continue; - } - ret = ast_strdup(app->name); - break; - } - /* Not in container. */ - break; - } - AST_RWLIST_UNLOCK(&apps); - - return ret; -} - static int hint_hash(const void *obj, const int flags) { const struct ast_hint *hint = obj; @@ -10659,7 +8586,6 @@ static void pbx_shutdown(void) if (contexts_table) { ast_hashtab_destroy(contexts_table, NULL); } - pbx_builtin_clear_globals(); } static void print_hints_key(void *v_obj, void *where, ao2_prnt_fn *prnt) |