2002-10-22 15:31:47 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2002-10-22 15:31:47 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2002-10-22 15:31:47 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2002-10-22 15:31:47 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Channel Variables
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
2012-03-22 19:51:16 +00:00
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2002-10-22 15:31:47 +00:00
|
|
|
*/
|
|
|
|
|
2012-06-15 16:20:16 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/chanvars.h"
|
2005-10-05 21:11:17 +00:00
|
|
|
#include "asterisk/strings.h"
|
2006-02-15 01:48:54 +00:00
|
|
|
#include "asterisk/utils.h"
|
2002-10-22 15:31:47 +00:00
|
|
|
|
2016-08-19 23:19:28 +00:00
|
|
|
#ifdef __AST_DEBUG_MALLOC
|
2008-10-14 22:38:06 +00:00
|
|
|
struct ast_var_t *_ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function)
|
|
|
|
#else
|
2004-10-24 02:53:24 +00:00
|
|
|
struct ast_var_t *ast_var_assign(const char *name, const char *value)
|
2008-10-14 22:38:06 +00:00
|
|
|
#endif
|
2012-03-22 19:51:16 +00:00
|
|
|
{
|
2002-07-26 15:44:05 +00:00
|
|
|
struct ast_var_t *var;
|
2006-02-15 01:48:54 +00:00
|
|
|
int name_len = strlen(name) + 1;
|
|
|
|
int value_len = strlen(value) + 1;
|
2002-10-22 15:31:47 +00:00
|
|
|
|
2016-08-19 23:19:28 +00:00
|
|
|
#ifdef __AST_DEBUG_MALLOC
|
2008-10-14 22:38:06 +00:00
|
|
|
if (!(var = __ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char), file, lineno, function))) {
|
|
|
|
#else
|
2006-02-15 01:48:54 +00:00
|
|
|
if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
|
2008-10-14 22:38:06 +00:00
|
|
|
#endif
|
2002-10-22 15:31:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-15 01:48:54 +00:00
|
|
|
ast_copy_string(var->name, name, name_len);
|
|
|
|
var->value = var->name + name_len;
|
|
|
|
ast_copy_string(var->value, value, value_len);
|
2012-03-22 19:51:16 +00:00
|
|
|
|
2002-10-22 15:31:47 +00:00
|
|
|
return var;
|
2012-03-22 19:51:16 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 15:31:47 +00:00
|
|
|
void ast_var_delete(struct ast_var_t *var)
|
|
|
|
{
|
2013-08-28 16:09:12 +00:00
|
|
|
ast_free(var);
|
2002-07-26 15:44:05 +00:00
|
|
|
}
|
|
|
|
|
2005-12-03 19:25:33 +00:00
|
|
|
const char *ast_var_name(const struct ast_var_t *var)
|
2004-11-01 02:23:28 +00:00
|
|
|
{
|
2005-12-03 19:25:33 +00:00
|
|
|
const char *name;
|
2004-11-01 02:23:28 +00:00
|
|
|
|
2005-12-03 19:25:33 +00:00
|
|
|
if (var == NULL || (name = var->name) == NULL)
|
2004-11-01 02:23:28 +00:00
|
|
|
return NULL;
|
|
|
|
/* Return the name without the initial underscores */
|
2005-12-03 19:25:33 +00:00
|
|
|
if (name[0] == '_') {
|
|
|
|
name++;
|
|
|
|
if (name[0] == '_')
|
|
|
|
name++;
|
|
|
|
}
|
2004-11-01 02:23:28 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2005-12-03 19:25:33 +00:00
|
|
|
const char *ast_var_full_name(const struct ast_var_t *var)
|
2002-10-22 15:31:47 +00:00
|
|
|
{
|
2005-10-05 21:11:17 +00:00
|
|
|
return (var ? var->name : NULL);
|
2002-07-26 15:44:05 +00:00
|
|
|
}
|
|
|
|
|
2005-12-03 19:25:33 +00:00
|
|
|
const char *ast_var_value(const struct ast_var_t *var)
|
2002-10-22 15:31:47 +00:00
|
|
|
{
|
2005-10-05 21:11:17 +00:00
|
|
|
return (var ? var->value : NULL);
|
2002-07-26 15:44:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:46:23 +00:00
|
|
|
char *ast_var_find(const struct varshead *head, const char *name)
|
|
|
|
{
|
|
|
|
struct ast_var_t *var;
|
|
|
|
|
|
|
|
AST_LIST_TRAVERSE(head, var, entries) {
|
|
|
|
if (!strcmp(name, var->name)) {
|
|
|
|
return var->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct varshead *ast_var_list_create(void)
|
|
|
|
{
|
|
|
|
struct varshead *head;
|
|
|
|
|
|
|
|
head = ast_calloc(1, sizeof(*head));
|
|
|
|
if (!head) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
AST_LIST_HEAD_INIT_NOLOCK(head);
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_var_list_destroy(struct varshead *head)
|
|
|
|
{
|
|
|
|
struct ast_var_t *var;
|
|
|
|
|
|
|
|
if (!head) {
|
|
|
|
return;
|
|
|
|
}
|
2005-10-05 21:11:17 +00:00
|
|
|
|
2014-10-09 17:46:23 +00:00
|
|
|
while ((var = AST_LIST_REMOVE_HEAD(head, entries))) {
|
|
|
|
ast_var_delete(var);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_free(head);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct varshead *ast_var_list_clone(struct varshead *head)
|
|
|
|
{
|
|
|
|
struct varshead *clone;
|
|
|
|
struct ast_var_t *var, *newvar;
|
|
|
|
|
|
|
|
if (!head) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone = ast_var_list_create();
|
|
|
|
if (!clone) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_VAR_LIST_TRAVERSE(head, var) {
|
|
|
|
newvar = ast_var_assign(var->name, var->value);
|
|
|
|
if (!newvar) {
|
|
|
|
ast_var_list_destroy(clone);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
AST_VAR_LIST_INSERT_TAIL(clone, newvar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|