[Asterisk-code-review] app_stack: Adds conditional return application (asterisk[master])

N A asteriskteam at digium.com
Sat Jun 26 16:37:36 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16077 )


Change subject: app_stack: Adds conditional return application
......................................................................

app_stack: Adds conditional return application

Adds the ReturnIf application, which conditionally
returns from a gosub subroutine and works similarly
to other conditional applications.

ASTERISK-29493

Change-Id: I046b778bd98a2aa97bd58b058ee77f7504407425
---
M apps/app_stack.c
A doc/CHANGES-staging/app_stack.txt
2 files changed, 72 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/77/16077/1

diff --git a/apps/app_stack.c b/apps/app_stack.c
index 179694b..2c896a6 100644
--- a/apps/app_stack.c
+++ b/apps/app_stack.c
@@ -113,6 +113,36 @@
 			any, is saved in the channel variable <variable>GOSUB_RETVAL</variable>.</para>
 		</description>
 		<see-also>
+			<ref type="application">ReturnIf</ref>
+			<ref type="application">Gosub</ref>
+			<ref type="application">StackPop</ref>
+		</see-also>
+	</application>
+	<application name="ReturnIf" language="en_US">
+		<synopsis>
+			Conditionally return from gosub routine.
+		</synopsis>
+		<syntax argsep="?">
+			<parameter name="expression" required="true" />
+			<parameter name="execapp" required="true" argsep=":">
+				<argument name="valueiftrue" required="true" hasparams="true">
+					<argument name="args" required="true" />
+				</argument>
+				<argument name="valueiffalse" required="false" hasparams="true">
+					<argument name="args" required="true" />
+				</argument>
+			</parameter>
+		</syntax>
+		<description>
+			<para>If <replaceable>expression</replaceable> is true, jumps to the last label on the stack,
+			removing it. The return <replaceable>valueiftrue</replaceable>, if any, is saved in the channel
+			variable <variable>GOSUB_RETVAL</variable>.</para> If <replaceable>expression</replaceable> is
+			false, and <replaceable>valueiffalse</replaceable> is specified, jumps to the last
+			label on the stack, removing it, and saving <replaceable>valueiffalse</replaceable>
+			in the the channel variable <variable>GOSUB_RETVAL</variable>.
+		</description>
+		<see-also>
+			<ref type="application">Return</ref>
 			<ref type="application">Gosub</ref>
 			<ref type="application">StackPop</ref>
 		</see-also>
@@ -232,6 +262,7 @@
 static const char app_gosub[] = "Gosub";
 static const char app_gosubif[] = "GosubIf";
 static const char app_return[] = "Return";
+static const char app_returnif[] = "ReturnIf";
 static const char app_pop[] = "StackPop";
 
 static void gosub_free(void *data);
@@ -428,6 +459,42 @@
 	return res;
 }
 
+static int returnif_exec(struct ast_channel *chan, const char *data)
+{
+	char *args;
+	int res=0;
+	AST_DECLARE_APP_ARGS(cond,
+		AST_APP_ARG(ition);
+		AST_APP_ARG(values);
+	);
+	AST_DECLARE_APP_ARGS(value,
+		AST_APP_ARG(iftrue);
+		AST_APP_ARG(iffalse);
+	);
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "ReturnIf requires an argument: ReturnIf(cond?value1:value2\n");
+		return 0;
+	}
+
+	args = ast_strdupa(data);
+	AST_NONSTANDARD_RAW_ARGS(cond, args, '?');
+	if (cond.argc != 2) {
+		ast_log(LOG_WARNING, "ReturnIf requires an argument: ReturnIf(cond?value1:value2\n");
+		return 0;
+	}
+
+	AST_NONSTANDARD_RAW_ARGS(value, cond.values, ':');
+
+	if (pbx_checkcondition(cond.ition)) {
+		res = return_exec(chan, value.iftrue);
+	} else if (!ast_strlen_zero(value.iffalse)) {
+		res = return_exec(chan, value.iffalse);
+	}
+
+	return res;
+}
+
 /*!
  * \internal
  * \brief Add missing context and/or exten to Gosub application argument string.
@@ -1282,6 +1349,7 @@
 	ast_agi_unregister(&gosub_agi_command);
 
 	ast_unregister_application(app_return);
+	ast_unregister_application(app_returnif);
 	ast_unregister_application(app_pop);
 	ast_unregister_application(app_gosubif);
 	ast_unregister_application(app_gosub);
@@ -1304,6 +1372,7 @@
 
 	ast_register_application_xml(app_pop, pop_exec);
 	ast_register_application_xml(app_return, return_exec);
+	ast_register_application_xml(app_returnif, returnif_exec);
 	ast_register_application_xml(app_gosubif, gosubif_exec);
 	ast_register_application_xml(app_gosub, gosub_exec);
 	ast_custom_function_register(&local_function);
diff --git a/doc/CHANGES-staging/app_stack.txt b/doc/CHANGES-staging/app_stack.txt
new file mode 100644
index 0000000..170d9af
--- /dev/null
+++ b/doc/CHANGES-staging/app_stack.txt
@@ -0,0 +1,3 @@
+Subject: ReturnIf application
+
+Adds a conditional return application.

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16077
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I046b778bd98a2aa97bd58b058ee77f7504407425
Gerrit-Change-Number: 16077
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210626/4e286da7/attachment.html>


More information about the asterisk-code-review mailing list