From 3b5fccac94b3b183dac1e87aca5f00280ee34ba0 Mon Sep 17 00:00:00 2001 From: Luigi Rizzo <rizzo@icir.org> Date: Mon, 8 May 2006 22:15:31 +0000 Subject: [PATCH] remove some common code in parsing function calls git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@25747 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- pbx.c | 65 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/pbx.c b/pbx.c index 527b2ef8e9..e7777be04a 100644 --- a/pbx.c +++ b/pbx.c @@ -1197,58 +1197,51 @@ int ast_custom_function_register(struct ast_custom_function *acf) return 0; } -int ast_func_read(struct ast_channel *chan, char *function, char *workspace, size_t len) +/*! \brief return a pointer to the arguments of the function, + * and terminates the function name with '\0' + */ +static char *func_args(char *function) { - char *args = NULL, *p; - struct ast_custom_function *acfptr; + char *args = strchr(function, '('); - if ((args = strchr(function, '('))) { + if (!args) + ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n"); + else { + char *p; *args++ = '\0'; - if ((p = strrchr(args, ')'))) + if ((p = strrchr(args, ')')) ) *p = '\0'; else ast_log(LOG_WARNING, "Can't find trailing parenthesis?\n"); - } else { - ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n"); } + return args; +} - if ((acfptr = ast_custom_function_find(function))) { - /* run the custom function */ - if (acfptr->read) - return acfptr->read(chan, function, args, workspace, len); - else - ast_log(LOG_ERROR, "Function %s cannot be read\n", function); - } else { - ast_log(LOG_ERROR, "Function %s not registered\n", function); - } +int ast_func_read(struct ast_channel *chan, char *function, char *workspace, size_t len) +{ + char *args = func_args(function); + struct ast_custom_function *acfptr = ast_custom_function_find(function); + if (acfptr == NULL) + ast_log(LOG_ERROR, "Function %s not registered\n", function); + else if (!acfptr->read) + ast_log(LOG_ERROR, "Function %s cannot be read\n", function); + else + return acfptr->read(chan, function, args, workspace, len); return -1; } int ast_func_write(struct ast_channel *chan, char *function, const char *value) { - char *args = NULL, *p; - struct ast_custom_function *acfptr; + char *args = func_args(function); + struct ast_custom_function *acfptr = ast_custom_function_find(function); - if ((args = strchr(function, '('))) { - *args++ = '\0'; - if ((p = strrchr(args, ')'))) - *p = '\0'; - else - ast_log(LOG_WARNING, "Can't find trailing parenthesis?\n"); - } else { - ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n"); - } - - if ((acfptr = ast_custom_function_find(function))) { - /* run the custom function */ - if (acfptr->write) - return acfptr->write(chan, function, args, value); - else - ast_log(LOG_ERROR, "Function %s is read-only, it cannot be written to\n", function); - } else { + if (acfptr == NULL) ast_log(LOG_ERROR, "Function %s not registered\n", function); - } + else if (!acfptr->write) + ast_log(LOG_ERROR, "Function %s cannot be written to\n", function); + else + return acfptr->write(chan, function, args, value); return -1; } -- GitLab