revert my pass through the tree to remove checks of the result of ast_strdupa
[asterisk/asterisk.git] / apps / app_externalivr.c
old mode 100755 (executable)
new mode 100644 (file)
index 59fc953..1023204
@@ -1,23 +1,40 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * External IVR application interface
- * 
- * Copyright (C) 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Kevin P. Fleming <kpfleming@digium.com>
  *
  * Portions taken from the file-based music-on-hold work
  * created by Anthony Minessale II in res_musiconhold.c
  *
+ * 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.
+ *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief External IVR application interface
+ *
+ * \author Kevin P. Fleming <kpfleming@digium.com>
+ *
+ * \note Portions taken from the file-based music-on-hold work
+ * created by Anthony Minessale II in res_musiconhold.c
+ *
+ * \ingroup applications
  */
 
 #include <stdlib.h>
-#include <unistd.h>
+#include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 
 #include "asterisk.h"
@@ -31,6 +48,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
 #include "asterisk/linkedlists.h"
+#include "asterisk/app.h"
+#include "asterisk/utils.h"
 
 static const char *tdesc = "External IVR Interface Application";
 
@@ -48,7 +67,8 @@ static const char *descrip =
 "when the channel is hung up.\n"
 "See doc/README.externalivr for a protocol specification.\n";
 
-#define ast_chan_log(level, channel, format, ...) ast_log(level, "%s: " format, channel->name, ## __VA_ARGS__)
+/* XXX the parser in gcc 2.95 gets confused if you don't put a space between 'name' and the comma */
+#define ast_chan_log(level, channel, format, ...) ast_log(level, "%s: " format, channel->name , ## __VA_ARGS__)
 
 struct playlist_entry {
        AST_LIST_ENTRY(playlist_entry) list;
@@ -60,7 +80,7 @@ struct localuser {
        struct localuser *next;
        AST_LIST_HEAD(playlist, playlist_entry) playlist;
        AST_LIST_HEAD(finishlist, playlist_entry) finishlist;
-       int list_cleared;
+       int abort_current_sound;
        int playing_silence;
        int option_autoclear;
 };
@@ -80,13 +100,12 @@ static void send_child_event(FILE *handle, const char event, const char *data,
        char tmp[256];
 
        if (!data) {
-               snprintf(tmp, sizeof(tmp), "%c,%10ld", event, time(NULL));
+               snprintf(tmp, sizeof(tmp), "%c,%10d", event, (int)time(NULL));
        } else {
-               snprintf(tmp, sizeof(tmp), "%c,%10ld,%s", event, time(NULL), data);
+               snprintf(tmp, sizeof(tmp), "%c,%10d,%s", event, (int)time(NULL), data);
        }
 
-       fputs(tmp, handle);
-       fputc('\n', handle);
+       fprintf(handle, "%s\n", tmp);
        ast_chan_log(LOG_DEBUG, chan, "sent '%s'\n", tmp);
 }
 
@@ -94,10 +113,8 @@ static void *gen_alloc(struct ast_channel *chan, void *params)
 {
        struct localuser *u = params;
        struct gen_state *state;
-
-       state = calloc(1, sizeof(*state));
-
-       if (!state)
+       
+       if (!(state = ast_calloc(1, sizeof(*state))))
                return NULL;
 
        state->u = u;
@@ -129,7 +146,7 @@ static int gen_nextfile(struct gen_state *state)
        struct localuser *u = state->u;
        char *file_to_stream;
        
-       u->list_cleared = 0;
+       u->abort_current_sound = 0;
        u->playing_silence = 0;
        gen_closestream(state);
 
@@ -160,7 +177,7 @@ static struct ast_frame *gen_readframe(struct gen_state *state)
        struct ast_frame *f = NULL;
        struct localuser *u = state->u;
        
-       if (u->list_cleared ||
+       if (u->abort_current_sound ||
            (u->playing_silence && AST_LIST_FIRST(&u->playlist))) {
                gen_closestream(state);
                AST_LIST_LOCK(&u->playlist);
@@ -216,10 +233,8 @@ static struct ast_generator gen =
 static struct playlist_entry *make_entry(const char *filename)
 {
        struct playlist_entry *entry;
-
-       entry = calloc(1, sizeof(*entry) + strlen(filename) + 10);
-
-       if (!entry)
+       
+       if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(filename) + 10)))
                return NULL;
 
        strcpy(entry->filename, filename);
@@ -238,27 +253,32 @@ static int app_exec(struct ast_channel *chan, void *data)
        int res = -1;
        int gen_active = 0;
        int pid;
-       char *command;
        char *argv[32];
        int argc = 1;
-       char *buf;
+       char *buf, *command;
        FILE *child_commands = NULL;
        FILE *child_errors = NULL;
        FILE *child_events = NULL;
 
-       if (!args || ast_strlen_zero(args)) {
+       LOCAL_USER_ADD(u);
+       
+       AST_LIST_HEAD_INIT(&u->playlist);
+       AST_LIST_HEAD_INIT(&u->finishlist);
+       u->abort_current_sound = 0;
+       
+       if (ast_strlen_zero(args)) {
                ast_log(LOG_WARNING, "ExternalIVR requires a command to execute\n");
                goto exit;
        }
 
        buf = ast_strdupa(data);
-       command = strsep(&buf, "|");
-       memset(argv, 0, sizeof(argv) / sizeof(argv[0]));
-       argv[0] = command;
-       while ((argc < 31) && (argv[argc++] = strsep(&buf, "|")));
-       argv[argc] = NULL;
+       if (!buf) {
+               ast_log(LOG_ERROR, "Out of memory!\n");
+               LOCAL_USER_REMOVE(u);
+               return -1;
+       }
 
-       LOCAL_USER_ADD(u);
+       argc = ast_app_separate_args(buf, '|', argv, sizeof(argv) / sizeof(argv[0]));
 
        if (pipe(child_stdin)) {
                ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child input: %s\n", strerror(errno));
@@ -275,10 +295,6 @@ static int app_exec(struct ast_channel *chan, void *data)
                goto exit;
        }
 
-       u->list_cleared = 0;
-       AST_LIST_HEAD_INIT(&u->playlist);
-       AST_LIST_HEAD_INIT(&u->finishlist);
-
        if (chan->_state != AST_STATE_UP) {
                ast_answer(chan);
        }
@@ -302,13 +318,10 @@ static int app_exec(struct ast_channel *chan, void *data)
                dup2(child_stdin[0], STDIN_FILENO);
                dup2(child_stdout[1], STDOUT_FILENO);
                dup2(child_stderr[1], STDERR_FILENO);
-               close(child_stdin[1]);
-               close(child_stdout[0]);
-               close(child_stderr[0]);
                for (i = STDERR_FILENO + 1; i < 1024; i++)
                        close(i);
-               execv(command, argv);
-               fprintf(stderr, "Failed to execute '%s': %s\n", command, strerror(errno));
+               execv(argv[0], argv);
+               fprintf(stderr, "Failed to execute '%s': %s\n", argv[0], strerror(errno));
                exit(1);
        } else {
                /* parent process */
@@ -323,16 +336,17 @@ static int app_exec(struct ast_channel *chan, void *data)
                struct ast_channel *rchan;
 
                close(child_stdin[0]);
+               child_stdin[0] = 0;
                close(child_stdout[1]);
+               child_stdout[1] = 0;
                close(child_stderr[1]);
+               child_stderr[1] = 0;
 
                if (!(child_events = fdopen(child_events_fd, "w"))) {
                        ast_chan_log(LOG_WARNING, chan, "Could not open stream for child events\n");
                        goto exit;
                }
 
-               setvbuf(child_events, NULL, _IONBF, 0);
-
                if (!(child_commands = fdopen(child_commands_fd, "r"))) {
                        ast_chan_log(LOG_WARNING, chan, "Could not open stream for child commands\n");
                        goto exit;
@@ -343,6 +357,10 @@ static int app_exec(struct ast_channel *chan, void *data)
                        goto exit;
                }
 
+               setvbuf(child_events, NULL, _IONBF, 0);
+               setvbuf(child_commands, NULL, _IONBF, 0);
+               setvbuf(child_errors, NULL, _IONBF, 0);
+
                res = 0;
 
                while (1) {
@@ -388,15 +406,15 @@ static int app_exec(struct ast_channel *chan, void *data)
                                if (f->frametype == AST_FRAME_DTMF) {
                                        send_child_event(child_events, f->subclass, NULL, chan);
                                        if (u->option_autoclear) {
-                                               if (!u->list_cleared && !u->playing_silence)
+                                               if (!u->abort_current_sound && !u->playing_silence)
                                                        send_child_event(child_events, 'T', NULL, chan);
                                                AST_LIST_LOCK(&u->playlist);
                                                while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
-                                                       if (!u->playing_silence)
-                                                               send_child_event(child_events, 'D', entry->filename, chan);
+                                                       send_child_event(child_events, 'D', entry->filename, chan);
                                                        free(entry);
                                                }
-                                               u->list_cleared = 1;
+                                               if (!u->playing_silence)
+                                                       u->abort_current_sound = 1;
                                                AST_LIST_UNLOCK(&u->playlist);
                                        }
                                } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
@@ -432,15 +450,15 @@ static int app_exec(struct ast_channel *chan, void *data)
                                                send_child_event(child_events, 'Z', NULL, chan);
                                                strcpy(&input[2], "exception");
                                        }
-                                       if (!u->list_cleared && !u->playing_silence)
+                                       if (!u->abort_current_sound && !u->playing_silence)
                                                send_child_event(child_events, 'T', NULL, chan);
                                        AST_LIST_LOCK(&u->playlist);
                                        while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
-                                               if (!u->playing_silence)
-                                                       send_child_event(child_events, 'D', entry->filename, chan);
+                                               send_child_event(child_events, 'D', entry->filename, chan);
                                                free(entry);
                                        }
-                                       u->list_cleared = 1;
+                                       if (!u->playing_silence)
+                                               u->abort_current_sound = 1;
                                        entry = make_entry(&input[2]);
                                        if (entry)
                                                AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
@@ -505,36 +523,41 @@ static int app_exec(struct ast_channel *chan, void *data)
        if (child_errors)
                fclose(child_errors);
 
-       if (child_stdin[0]) {
+       if (child_stdin[0])
                close(child_stdin[0]);
+
+       if (child_stdin[1])
                close(child_stdin[1]);
-       }
 
-       if (child_stdout[0]) {
+       if (child_stdout[0])
                close(child_stdout[0]);
+
+       if (child_stdout[1])
                close(child_stdout[1]);
-       }
 
-       if (child_stderr[0]) {
+       if (child_stderr[0])
                close(child_stderr[0]);
+
+       if (child_stderr[1])
                close(child_stderr[1]);
-       }
 
-       if (u) {
-               while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
-                       free(entry);
+       while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
+               free(entry);
 
-               LOCAL_USER_REMOVE(u);
-       }
+       LOCAL_USER_REMOVE(u);
 
        return res;
 }
 
 int unload_module(void)
 {
+       int res;
+
+       res = ast_unregister_application(app);
+
        STANDARD_HANGUP_LOCALUSERS;
 
-       return ast_unregister_application(app);
+       return res;
 }
 
 int load_module(void)