<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18562">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">pbx: Add pbx exec helper function.<br><br>Finding an application and executing it if found is<br>a common task throughout Asterisk. This adds a wrapper<br>function around pbx_exec to do this, to eliminate<br>redundant code and make it easier for modules to<br>execute applications by name.<br><br>ASTERISK-30061 #close<br><br>Change-Id: Ifee4d2825df7545fb515d763d393065675140c84<br>---<br>M apps/app_disa.c<br>M include/asterisk/pbx.h<br>M main/bridge_channel.c<br>M main/dial.c<br>M main/features.c<br>M main/pbx_app.c<br>M main/pbx_builtins.c<br>M res/res_stasis_snoop.c<br>8 files changed, 49 insertions(+), 49 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/62/18562/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/apps/app_disa.c b/apps/app_disa.c</span><br><span>index cceb554..7e69fec 100644</span><br><span>--- a/apps/app_disa.c</span><br><span>+++ b/apps/app_disa.c</span><br><span>@@ -361,7 +361,6 @@</span><br><span> </span><br><span>       if (k == 3) {</span><br><span>                int recheck = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-                struct ast_app *app_reset_cdr;</span><br><span> </span><br><span>           if (!ast_exists_extension(chan, args.context, exten, 1,</span><br><span>                      S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {</span><br><span>@@ -386,10 +385,7 @@</span><br><span>                             ast_channel_unlock(chan);</span><br><span>                    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   app_reset_cdr = pbx_findapp("ResetCDR");</span><br><span style="color: hsl(0, 100%, 40%);">-                      if (app_reset_cdr) {</span><br><span style="color: hsl(0, 100%, 40%);">-                            pbx_exec(chan, app_reset_cdr, special_noanswer ? "" : "e");</span><br><span style="color: hsl(0, 100%, 40%);">-                 } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (pbx_exec_helper(chan, "ResetCDR", special_noanswer ? "" : "e")) {</span><br><span>                          ast_log(AST_LOG_NOTICE, "ResetCDR application not found; CDR will not be reset\n");</span><br><span>                        }</span><br><span>                    ast_explicit_goto(chan, args.context, exten, 1);</span><br><span>diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h</span><br><span>index d531b44..d0082e2 100644</span><br><span>--- a/include/asterisk/pbx.h</span><br><span>+++ b/include/asterisk/pbx.h</span><br><span>@@ -269,6 +269,21 @@</span><br><span> int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Execute an application</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param c channel to execute on</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param app name of app to execute</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param data the data passed into the app</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This application executes an application by name on a given channel.</span><br><span style="color: hsl(120, 100%, 40%);">+ * It is a wrapper around pbx_exec that will execute the application if it exists.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 failure</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int pbx_exec_helper(struct ast_channel *chan, const char *app_name, const char *app_args);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span>  * \brief Register a new context or find an existing one</span><br><span>  *</span><br><span>  * \param extcontexts pointer to the ast_context structure pointer</span><br><span>diff --git a/main/bridge_channel.c b/main/bridge_channel.c</span><br><span>index 1b0f1dd..29edb6c 100644</span><br><span>--- a/main/bridge_channel.c</span><br><span>+++ b/main/bridge_channel.c</span><br><span>@@ -1178,23 +1178,7 @@</span><br><span>       } else if (!strcasecmp("Macro", app_name)) {</span><br><span>               ast_app_exec_macro(NULL, chan, app_args);</span><br><span>    } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                struct ast_app *app;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-            app = pbx_findapp(app_name);</span><br><span style="color: hsl(0, 100%, 40%);">-            if (!app) {</span><br><span style="color: hsl(0, 100%, 40%);">-                     ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);</span><br><span style="color: hsl(0, 100%, 40%);">-          } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        struct ast_str *substituted_args = ast_str_create(16);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-                  if (substituted_args) {</span><br><span style="color: hsl(0, 100%, 40%);">-                         ast_str_substitute_variables(&substituted_args, 0, chan, app_args);</span><br><span style="color: hsl(0, 100%, 40%);">-                         res = pbx_exec(chan, app, ast_str_buffer(substituted_args));</span><br><span style="color: hsl(0, 100%, 40%);">-                            ast_free(substituted_args);</span><br><span style="color: hsl(0, 100%, 40%);">-                     } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                                ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);</span><br><span style="color: hsl(0, 100%, 40%);">-                               res = pbx_exec(chan, app, app_args);</span><br><span style="color: hsl(0, 100%, 40%);">-                    }</span><br><span style="color: hsl(0, 100%, 40%);">-               }</span><br><span style="color: hsl(120, 100%, 40%);">+             res = pbx_exec_helper(chan, app_name, app_args);</span><br><span>     }</span><br><span>    return res;</span><br><span> }</span><br><span>diff --git a/main/dial.c b/main/dial.c</span><br><span>index c40b7fb..256816e 100644</span><br><span>--- a/main/dial.c</span><br><span>+++ b/main/dial.c</span><br><span>@@ -166,14 +166,12 @@</span><br><span> static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)</span><br><span> {</span><br><span>   struct ast_channel *chan = dial_channel->owner;</span><br><span style="color: hsl(0, 100%, 40%);">-      struct ast_app *ast_app = pbx_findapp(app);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* If the application was not found, return immediately */</span><br><span style="color: hsl(0, 100%, 40%);">-      if (!ast_app)</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Execute the application, if available */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (pbx_exec_helper(chan, app, args)) {</span><br><span style="color: hsl(120, 100%, 40%);">+               /* If the application was not found, return immediately */</span><br><span>           return;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- /* All is well... execute the application */</span><br><span style="color: hsl(0, 100%, 40%);">-    pbx_exec(chan, ast_app, args);</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span> </span><br><span>        /* If another thread is not taking over hang up the channel */</span><br><span>       ast_mutex_lock(&dial->lock);</span><br><span>diff --git a/main/features.c b/main/features.c</span><br><span>index b67bf38..3d82496 100644</span><br><span>--- a/main/features.c</span><br><span>+++ b/main/features.c</span><br><span>@@ -504,12 +504,7 @@</span><br><span>          ast_channel_unlock(peer);</span><br><span>    }</span><br><span>    if (monitor_chan) {</span><br><span style="color: hsl(0, 100%, 40%);">-             struct ast_app *monitor_app;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-            monitor_app = pbx_findapp("Monitor");</span><br><span style="color: hsl(0, 100%, 40%);">-         if (monitor_app) {</span><br><span style="color: hsl(0, 100%, 40%);">-                      pbx_exec(monitor_chan, monitor_app, monitor_args);</span><br><span style="color: hsl(0, 100%, 40%);">-              }</span><br><span style="color: hsl(120, 100%, 40%);">+             pbx_exec_helper(monitor_chan, "Monitor", monitor_args);</span><br><span>    }</span><br><span> }</span><br><span> </span><br><span>diff --git a/main/pbx_app.c b/main/pbx_app.c</span><br><span>index 0cbb04a..bf5cb34 100644</span><br><span>--- a/main/pbx_app.c</span><br><span>+++ b/main/pbx_app.c</span><br><span>@@ -498,6 +498,31 @@</span><br><span>       return res;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int pbx_exec_helper(struct ast_channel *chan, const char *app_name, const char *app_args)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    int res = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_app *app;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        app = pbx_findapp(app_name);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (!app) {</span><br><span style="color: hsl(120, 100%, 40%);">+           ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);</span><br><span style="color: hsl(120, 100%, 40%);">+        } else {</span><br><span style="color: hsl(120, 100%, 40%);">+              struct ast_str *substituted_args = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+            if (!ast_strlen_zero(app_args) && (substituted_args = ast_str_create(16))) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  ast_str_substitute_variables(&substituted_args, 0, chan, app_args);</span><br><span style="color: hsl(120, 100%, 40%);">+                       res = pbx_exec(chan, app, ast_str_buffer(substituted_args));</span><br><span style="color: hsl(120, 100%, 40%);">+                  ast_free(substituted_args);</span><br><span style="color: hsl(120, 100%, 40%);">+           } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (!ast_strlen_zero(app_args)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+                     res = pbx_exec(chan, app, app_args);</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static struct ast_cli_entry app_cli[] = {</span><br><span>        AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),</span><br><span>        AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),</span><br><span>diff --git a/main/pbx_builtins.c b/main/pbx_builtins.c</span><br><span>index 078a307..91a2045 100644</span><br><span>--- a/main/pbx_builtins.c</span><br><span>+++ b/main/pbx_builtins.c</span><br><span>@@ -1000,7 +1000,6 @@</span><br><span> {</span><br><span>         char *s, *appname;</span><br><span>   struct ast_timing timing;</span><br><span style="color: hsl(0, 100%, 40%);">-       struct ast_app *app;</span><br><span>         static const char * const usage = "ExecIfTime requires an argument:\n  <time range>,<days of week>,<days of month>,<months>[,<timezone>]?<appname>[(<appargs>)]";</span><br><span> </span><br><span>    if (ast_strlen_zero(data)) {</span><br><span>@@ -1038,13 +1037,7 @@</span><br><span>                        ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-   if ((app = pbx_findapp(appname))) {</span><br><span style="color: hsl(0, 100%, 40%);">-             return pbx_exec(chan, app, S_OR(s, ""));</span><br><span style="color: hsl(0, 100%, 40%);">-      } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                ast_log(LOG_WARNING, "Cannot locate application %s\n", appname);</span><br><span style="color: hsl(0, 100%, 40%);">-              return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-      }</span><br><span style="color: hsl(120, 100%, 40%);">+     return pbx_exec_helper(chan, appname, S_OR(s, ""));</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span>diff --git a/res/res_stasis_snoop.c b/res/res_stasis_snoop.c</span><br><span>index 190ee06..89ecd42 100644</span><br><span>--- a/res/res_stasis_snoop.c</span><br><span>+++ b/res/res_stasis_snoop.c</span><br><span>@@ -263,14 +263,8 @@</span><br><span> static void *snoop_stasis_thread(void *obj)</span><br><span> {</span><br><span>        RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);</span><br><span style="color: hsl(0, 100%, 40%);">-   struct ast_app *stasis = pbx_findapp("Stasis");</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   if (!stasis) {</span><br><span style="color: hsl(0, 100%, 40%);">-          ast_hangup(snoop->chan);</span><br><span style="color: hsl(0, 100%, 40%);">-             return NULL;</span><br><span style="color: hsl(0, 100%, 40%);">-    }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));</span><br><span style="color: hsl(120, 100%, 40%);">+      pbx_exec_helper(snoop->chan, "Stasis", ast_str_buffer(snoop->app));</span><br><span> </span><br><span>      ast_hangup(snoop->chan);</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18562">change 18562</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18562"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Ifee4d2825df7545fb515d763d393065675140c84 </div>
<div style="display:none"> Gerrit-Change-Number: 18562 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>