Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / apps / app_festival.c
old mode 100755 (executable)
new mode 100644 (file)
index 91a23c1..6a8a26f
@@ -1,26 +1,37 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Connect to festival
- * 
  * Copyright (C) 2002, Christos Ricudis
  *
  * Christos Ricudis <ricudis@itc.auth.gr>
  *
+ * 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 Connect to festival
+ *
+ * \author Christos Ricudis <ricudis@itc.auth.gr>
+ *
+ * \extref  The Festival Speech Synthesis System - http://www.cstr.ed.ac.uk/projects/festival/
+ * 
+ * \ingroup applications
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <sys/types.h>
-#include <asterisk/file.h>
-#include <asterisk/logger.h>
-#include <asterisk/channel.h>
-#include <asterisk/pbx.h>
-#include <asterisk/module.h>
-#include <asterisk/md5.h>
-#include <asterisk/config.h>
-#include <asterisk/utils.h>
-#include <asterisk/lock.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <fcntl.h>
 #include <ctype.h>
 
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/md5.h"
+#include "asterisk/config.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/options.h"
 
 #define FESTIVAL_CONFIG "festival.conf"
 
-static char *tdesc = "Simple Festival Interface";
-
 static char *app = "Festival";
 
 static char *synopsis = "Say text to the user";
@@ -51,15 +70,12 @@ static char *descrip =
 "play it to the user, allowing any given interrupt keys to immediately terminate and return\n"
 "the value, or 'any' to allow any number back (useful in dialplan)\n";
 
-STANDARD_LOCAL_USER;
-
-LOCAL_USER_DECL;
 
 static char *socket_receive_file_to_buff(int fd,int *size)
 {
     /* Receive file (probably a waveform file) from socket using   */
     /* Festival key stuff technique, but long winded I know, sorry */
-    /* but will receive any file without closeing the stream or    */
+    /* but will receive any file without closing the stream or    */
     /* using OOB data                                              */
     static char *file_stuff_key = "ft_StUfF_key"; /* must == Festival's key */
     char *buff;
@@ -68,7 +84,10 @@ static char *socket_receive_file_to_buff(int fd,int *size)
     char c;
 
     bufflen = 1024;
-    buff = (char *)malloc(bufflen);
+    if (!(buff = ast_malloc(bufflen)))
+    {
+        /* TODO: Handle memory allocation failure */
+    }
     *size=0;
 
     for (k=0; file_stuff_key[k] != '\0';)
@@ -78,7 +97,10 @@ static char *socket_receive_file_to_buff(int fd,int *size)
         if ((*size)+k+1 >= bufflen)
         {   /* +1 so you can add a NULL if you want */
             bufflen += bufflen/4;
-            buff = (char *)realloc(buff,bufflen);
+            if (!(buff = ast_realloc(buff, bufflen)))
+            {
+                /* TODO: Handle memory allocation failure */
+            }
         }
         if (file_stuff_key[k] == c)
             k++;
@@ -110,16 +132,26 @@ static int send_waveform_to_fd(char *waveform, int length, int fd) {
 #ifdef __PPC__ 
        char c;
 #endif
+       sigset_t fullset, oldset;
+
+       sigfillset(&fullset);
+       pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
 
         res = fork();
         if (res < 0)
                 ast_log(LOG_WARNING, "Fork failed\n");
-        if (res)
+        if (res) {
+               pthread_sigmask(SIG_SETMASK, &oldset, NULL);
                 return res;
+       }
         for (x=0;x<256;x++) {
                 if (x != fd)
                         close(x);
         }
+       if (ast_opt_high_priority)
+               ast_set_priority(0);
+       signal(SIGPIPE, SIG_DFL);
+       pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
 /*IAS */
 #ifdef __PPC__  
        for( x=0; x<length; x+=2)
@@ -131,7 +163,6 @@ static int send_waveform_to_fd(char *waveform, int length, int fd) {
 #endif
        
        write(fd,waveform,length);
-       write(fd,"a",1);
        close(fd);
        exit(0);
 }
@@ -149,7 +180,9 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                struct ast_frame f;
                char offset[AST_FRIENDLY_OFFSET];
                char frdata[2048];
-       } myf;
+       } myf = {
+               .f = { 0, },
+       };
        
         if (pipe(fds)) {
                  ast_log(LOG_WARNING, "Unable to create pipe\n");
@@ -160,7 +193,8 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
        if (chan->_state != AST_STATE_UP)
                ast_answer(chan);
        ast_stopstream(chan);
-
+       ast_indicate(chan, -1);
+       
        owriteformat = chan->writeformat;
        res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
        if (res < 0) {
@@ -187,7 +221,7 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                break;
                        }
                        if (f->frametype == AST_FRAME_DTMF) {
-                               ast_log(LOG_DEBUG, "User pressed a key\n");
+                               ast_debug(1, "User pressed a key\n");
                                if (intkeys && strchr(intkeys, f->subclass)) {
                                        res = f->subclass;
                                        ast_frfree(f);
@@ -208,21 +242,22 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                        myf.f.subclass = AST_FORMAT_SLINEAR;
                                        myf.f.datalen = res;
                                        myf.f.samples = res / 2;
-                                       myf.f.mallocd = 0;
                                        myf.f.offset = AST_FRIENDLY_OFFSET;
                                        myf.f.src = __PRETTY_FUNCTION__;
                                        myf.f.data = myf.frdata;
                                        if (ast_write(chan, &myf.f) < 0) {
                                                res = -1;
+                                               ast_frfree(f);
                                                break;
                                        }
                                        if (res < needed) { /* last frame */
-                                               ast_log(LOG_DEBUG, "Last frame\n");
+                                               ast_debug(1, "Last frame\n");
                                                res=0;
+                                               ast_frfree(f);
                                                break;
                                        }
                                } else {
-                                       ast_log(LOG_DEBUG, "No more waveform\n");
+                                       ast_debug(1, "No more waveform\n");
                                        res = 0;
                                }
                        }
@@ -249,16 +284,16 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
 {
        int usecache;
        int res=0;
-       struct localuser *u;
+       struct ast_module_user *u;
        struct sockaddr_in serv_addr;
        struct hostent *serverhost;
        struct ast_hostent ahp;
        int fd;
        FILE *fs;
-       char *host;
-       char *cachedir;
-       char *temp;
-       char *festivalcommand;
+       const char *host;
+       const char *cachedir;
+       const char *temp;
+       const char *festivalcommand;
        int port=1314;
        int n;
        char ack[4];
@@ -278,13 +313,22 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        int fdesc = -1;
        char buffer[16384];
        int seekpos = 0;        
-       char data[256] = "";
+       char *data;     
        char *intstr;
-       
        struct ast_config *cfg;
+       char *newfestivalcommand;
+
+       if (ast_strlen_zero(vdata)) {
+               ast_log(LOG_WARNING, "festival requires an argument (text)\n");
+               return -1;
+       }
+
+       u = ast_module_user_add(chan);
+
        cfg = ast_config_load(FESTIVAL_CONFIG);
        if (!cfg) {
                ast_log(LOG_WARNING, "No such configuration file %s\n", FESTIVAL_CONFIG);
+               ast_module_user_remove(u);
                return -1;
        }
        if (!(host = ast_variable_retrieve(cfg, "general", "host"))) {
@@ -305,47 +349,68 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        }
        if (!(festivalcommand = ast_variable_retrieve(cfg, "general", "festivalcommand"))) {
                festivalcommand = "(tts_textasterisk \"%s\" 'file)(quit)\n";
+       } else { /* This else parses the festivalcommand that we're sent from the config file for \n's, etc */
+               int i, j;
+               newfestivalcommand = alloca(strlen(festivalcommand) + 1);
+
+               for (i = 0, j = 0; i < strlen(festivalcommand); i++) {
+                       if (festivalcommand[i] == '\\' && festivalcommand[i + 1] == 'n') {
+                               newfestivalcommand[j++] = '\n';
+                               i++;
+                       } else if (festivalcommand[i] == '\\') {
+                               newfestivalcommand[j++] = festivalcommand[i + 1];
+                               i++;
+                       } else
+                               newfestivalcommand[j++] = festivalcommand[i];
+               }
+               newfestivalcommand[j] = '\0';
+               festivalcommand = newfestivalcommand;
        }
-       if (!vdata || ast_strlen_zero(vdata)) {
-               ast_log(LOG_WARNING, "festival requires an argument (text)\n");
-               ast_config_destroy(cfg);
-               return -1;
-       }
-       strncpy(data, vdata, sizeof(data) - 1);
-       if ((intstr = strchr(data, '|'))) {
+       
+       data = ast_strdupa(vdata);
+
+       intstr = strchr(data, '|');
+       if (intstr) {   
                *intstr = '\0';
                intstr++;
                if (!strcasecmp(intstr, "any"))
                        intstr = AST_DIGIT_ANY;
        }
-       LOCAL_USER_ADD(u);
-       ast_log(LOG_DEBUG, "Text passed to festival server : %s\n",(char *)data);
+       
+       ast_debug(1, "Text passed to festival server : %s\n",(char *)data);
        /* Connect to local festival server */
        
-       fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+       fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
-       if (fd < 0) {
+       if (fd < 0) {
                ast_log(LOG_WARNING,"festival_client: can't get socket\n");
                ast_config_destroy(cfg);
-               return -1;
+               ast_module_user_remove(u);
+               return -1;
        }
-        memset(&serv_addr, 0, sizeof(serv_addr));
-        if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
-               /* its a name rather than an ipnum */
-               serverhost = ast_gethostbyname(host, &ahp);
-               if (serverhost == (struct hostent *)0) {
-                       ast_log(LOG_WARNING,"festival_client: gethostbyname failed\n");
+
+       memset(&serv_addr, 0, sizeof(serv_addr));
+
+       if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
+               /* its a name rather than an ipnum */
+               serverhost = ast_gethostbyname(host, &ahp);
+
+               if (serverhost == (struct hostent *)0) {
+                       ast_log(LOG_WARNING,"festival_client: gethostbyname failed\n");
                        ast_config_destroy(cfg);
-                       return -1;
-               }
-               memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
-       }
+                       ast_module_user_remove(u);
+                       return -1;
+               }
+               memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
+       }
+
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(port);
 
        if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
                ast_log(LOG_WARNING,"festival_client: connect to server failed\n");
                ast_config_destroy(cfg);
+               ast_module_user_remove(u);
                return -1;
        }
        
@@ -367,21 +432,21 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5Hex);
                fdesc=open(cachefile,O_RDWR);
                if (fdesc==-1) {
-                       fdesc=open(cachefile,O_CREAT|O_RDWR,0777);
+                       fdesc=open(cachefile,O_CREAT|O_RDWR,AST_FILE_MODE);
                        if (fdesc!=-1) {
                                writecache=1;
                                strln=strlen((char *)data);
-                               ast_log(LOG_DEBUG,"line length : %d\n",strln);
+                               ast_debug(1,"line length : %d\n",strln);
                                write(fdesc,&strln,sizeof(int));
                                write(fdesc,data,strln);
                                seekpos=lseek(fdesc,0,SEEK_CUR);
-                               ast_log(LOG_DEBUG,"Seek position : %d\n",seekpos);
+                               ast_debug(1,"Seek position : %d\n",seekpos);
                        }
                } else {
                        read(fdesc,&strln,sizeof(int));
-                       ast_log(LOG_DEBUG,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
+                       ast_debug(1,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
                        if (strlen((char *)data)==strln) {
-                               ast_log(LOG_DEBUG,"Size OK\n");
+                               ast_debug(1,"Size OK\n");
                                read(fdesc,&bigstring,strln);
                                bigstring[strln] = 0;
                                if (strcmp(bigstring,data)==0) { 
@@ -398,10 +463,10 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        if (readcache==1) {
                close(fd);
                fd=fdesc;
-               ast_log(LOG_DEBUG,"Reading from cache...\n");
+               ast_debug(1,"Reading from cache...\n");
        } else {
-               ast_log(LOG_DEBUG,"Passing text to festival...\n");
-               fs=fdopen(dup(fd),"wb");
+               ast_debug(1,"Passing text to festival...\n");
+               fs=fdopen(dup(fd),"wb");
                fprintf(fs,festivalcommand,(char *)data);
                fflush(fs);
                fclose(fs);
@@ -409,7 +474,7 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        
        /* Write to cache and then pass it down */
        if (writecache==1) {
-               ast_log(LOG_DEBUG,"Writing result to cache...\n");
+               ast_debug(1,"Writing result to cache...\n");
                while ((strln=read(fd,buffer,16384))!=0) {
                        write(fdesc,buffer,strln);
                }
@@ -419,28 +484,43 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                lseek(fd,seekpos,SEEK_SET);
        }
        
-       ast_log(LOG_DEBUG,"Passing data to channel...\n");
+       ast_debug(1,"Passing data to channel...\n");
        
        /* Read back info from server */
        /* This assumes only one waveform will come back, also LP is unlikely */
        wave = 0;
        do {
+               int read_data;
                for (n=0; n < 3; )
-                       n += read(fd,ack+n,3-n);
+               {
+                       read_data = read(fd,ack+n,3-n);
+                       /* this avoids falling in infinite loop
+                        * in case that festival server goes down
+                        * */
+                       if ( read_data == -1 )
+                       {
+                               ast_log(LOG_WARNING,"Unable to read from cache/festival fd\n");
+                              close(fd);
+                              ast_config_destroy(cfg);
+                              ast_module_user_remove(u);
+                               return -1;
+                       }
+                       n += read_data;
+               }
                ack[3] = '\0';
                if (strcmp(ack,"WV\n") == 0) {         /* receive a waveform */
-                       ast_log(LOG_DEBUG,"Festival WV command\n");
+                       ast_debug(1,"Festival WV command\n");
                        waveform = socket_receive_file_to_buff(fd,&filesize);
                        res = send_waveform_to_channel(chan,waveform,filesize, intstr);
-                       free(waveform);
+                       ast_free(waveform);
                        break;
                }
                else if (strcmp(ack,"LP\n") == 0) {   /* receive an s-expr */
-                       ast_log(LOG_DEBUG,"Festival LP command\n");
+                       ast_debug(1,"Festival LP command\n");
                        waveform = socket_receive_file_to_buff(fd,&filesize);
                        waveform[filesize]='\0';
                        ast_log(LOG_WARNING,"Festival returned LP : %s\n",waveform);
-                       free(waveform);
+                       ast_free(waveform);
                } else if (strcmp(ack,"ER\n") == 0) {    /* server got an error */
                        ast_log(LOG_WARNING,"Festival returned ER\n");
                        res=-1;
@@ -449,36 +529,31 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        } while (strcmp(ack,"OK\n") != 0);
        close(fd);
        ast_config_destroy(cfg);
-       LOCAL_USER_REMOVE(u);                                                                                
+       ast_module_user_remove(u);
        return res;
 
 }
 
-int unload_module(void)
+static int unload_module(void)
 {
-       STANDARD_HANGUP_LOCALUSERS;
-       return ast_unregister_application(app);
-}
+       int res;
 
-int load_module(void)
-{
-       
-       return ast_register_application(app, festival_exec, synopsis, descrip);
-}
+       res = ast_unregister_application(app);
 
-char *description(void)
-{
-       return tdesc;
-}
+       ast_module_user_hangup_all();
 
-int usecount(void)
-{
-       int res;
-       STANDARD_USECOUNT(res);
        return res;
 }
 
-char *key()
+static int load_module(void)
 {
-       return ASTERISK_GPL_KEY;
+       struct ast_config *cfg = ast_config_load(FESTIVAL_CONFIG);
+       if (!cfg) {
+               ast_log(LOG_WARNING, "No such configuration file %s\n", FESTIVAL_CONFIG);
+               return AST_MODULE_LOAD_DECLINE;
+       }
+       ast_config_destroy(cfg);
+       return ast_register_application(app, festival_exec, synopsis, descrip);
 }
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Festival Interface");