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 58f2d77..6a8a26f
@@ -1,24 +1,37 @@
 /*
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
  *
- * Connect to festival
- * 
  * Copyright (C) 2002, Christos Ricudis
  *
  * Copyright (C) 2002, Christos Ricudis
  *
- * Christos Ricudis <ricudis@paiko.gr>
+ * 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
  *
  * 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 <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 <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <ctype.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <ctype.h>
-#include <pthread.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"
 
 
 #define FESTIVAL_CONFIG "festival.conf"
 
-static char *tdesc = "Simple Festival Interface";
-
 static char *app = "Festival";
 
 static char *synopsis = "Say text to the user";
 static char *app = "Festival";
 
 static char *synopsis = "Say text to the user";
@@ -48,17 +68,14 @@ static char *synopsis = "Say text to the user";
 static char *descrip = 
 "  Festival(text[|intkeys]):  Connect to Festival, send the argument, get back the waveform,"
 "play it to the user, allowing any given interrupt keys to immediately terminate and return\n"
 static char *descrip = 
 "  Festival(text[|intkeys]):  Connect to Festival, send the argument, get back the waveform,"
 "play it to the user, allowing any given interrupt keys to immediately terminate and return\n"
-"the value.\n";
-
-STANDARD_LOCAL_USER;
+"the value, or 'any' to allow any number back (useful in dialplan)\n";
 
 
-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 */
 
 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;
     /* using OOB data                                              */
     static char *file_stuff_key = "ft_StUfF_key"; /* must == Festival's key */
     char *buff;
@@ -67,7 +84,10 @@ static char *socket_receive_file_to_buff(int fd,int *size)
     char c;
 
     bufflen = 1024;
     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';)
     *size=0;
 
     for (k=0; file_stuff_key[k] != '\0';)
@@ -77,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;
         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++;
         }
         if (file_stuff_key[k] == c)
             k++;
@@ -106,50 +129,72 @@ static int send_waveform_to_fd(char *waveform, int length, int fd) {
 
         int res;
         int x;
 
         int res;
         int x;
+#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");
         res = fork();
         if (res < 0)
                 ast_log(LOG_WARNING, "Fork failed\n");
-        if (res)
+        if (res) {
+               pthread_sigmask(SIG_SETMASK, &oldset, NULL);
                 return res;
                 return res;
+       }
         for (x=0;x<256;x++) {
                 if (x != fd)
                         close(x);
         }
         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)
+       {
+               c = *(waveform+x+1);
+               *(waveform+x+1)=*(waveform+x);
+               *(waveform+x)=c;
+       }
+#endif
+       
        write(fd,waveform,length);
        write(fd,waveform,length);
-       write(fd,"a",1);
        close(fd);
        exit(0);
 }
 
 
        close(fd);
        exit(0);
 }
 
 
-static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, int length) {
+static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, int length, char *intkeys) {
        int res=0;
        int fds[2];
        int res=0;
        int fds[2];
-       int rfds[1 + AST_MAX_FDS];
        int ms = -1;
        int pid = -1;
        int ms = -1;
        int pid = -1;
-       int us;
-       int exception;
+       int needed = 0;
        int owriteformat;
        int owriteformat;
-       struct timeval tv;
-       struct timeval last;
        struct ast_frame *f;
        struct ast_frame *f;
-       int x;
        struct myframe {
                struct ast_frame f;
                char offset[AST_FRIENDLY_OFFSET];
        struct myframe {
                struct ast_frame f;
                char offset[AST_FRIENDLY_OFFSET];
-               char frdata[160];
-       } myf;
-       last.tv_usec = 0;
-       last.tv_sec = 0;
+               char frdata[2048];
+       } myf = {
+               .f = { 0, },
+       };
        
         if (pipe(fds)) {
                  ast_log(LOG_WARNING, "Unable to create pipe\n");
                return -1;
         }
                                                        
        
         if (pipe(fds)) {
                  ast_log(LOG_WARNING, "Unable to create pipe\n");
                return -1;
         }
                                                        
+       /* Answer if it's not already going */
+       if (chan->_state != AST_STATE_UP)
+               ast_answer(chan);
        ast_stopstream(chan);
        ast_stopstream(chan);
-
+       ast_indicate(chan, -1);
+       
        owriteformat = chan->writeformat;
        res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
        if (res < 0) {
        owriteformat = chan->writeformat;
        res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
        if (res < 0) {
@@ -162,86 +207,68 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                pid = res;
                /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
                   user */
                pid = res;
                /* Order is important -- there's almost always going to be mp3...  we want to prioritize the
                   user */
-               rfds[AST_MAX_FDS] = fds[0];
                for (;;) {
                for (;;) {
-                       CHECK_BLOCKING(chan);
-                       for (x=0;x<AST_MAX_FDS;x++) 
-                               rfds[x] = chan->fds[x];
-                       res = ast_waitfor_n_fd(rfds, AST_MAX_FDS+1, &ms, &exception);
-                       chan->blocking = 0;
+                       ms = 1000;
+                       res = ast_waitfor(chan, ms);
                        if (res < 1) {
                        if (res < 1) {
-                               ast_log(LOG_DEBUG, "Hangup detected\n");
                                res = -1;
                                break;
                        }
                                res = -1;
                                break;
                        }
-                       for(x=0;x<AST_MAX_FDS;x++) 
-                               if (res == chan->fds[x])
-                                       break;
-
-                       if (x < AST_MAX_FDS) {
-                               if (exception)
-                                       chan->exception = 1;
-                               f = ast_read(chan);
-                               if (!f) {
-                                       ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
-                                       res = -1;
-                                       break;
-                               }
-                               if (f->frametype == AST_FRAME_DTMF) {
-                                       ast_log(LOG_DEBUG, "User pressed a key\n");
+                       f = ast_read(chan);
+                       if (!f) {
+                               ast_log(LOG_WARNING, "Null frame == hangup() detected\n");
+                               res = -1;
+                               break;
+                       }
+                       if (f->frametype == AST_FRAME_DTMF) {
+                               ast_debug(1, "User pressed a key\n");
+                               if (intkeys && strchr(intkeys, f->subclass)) {
+                                       res = f->subclass;
                                        ast_frfree(f);
                                        ast_frfree(f);
-                                       res = 0;
                                        break;
                                }
                                        break;
                                }
-                               ast_frfree(f);
-                       } else if (res == fds[0]) {
-                               gettimeofday(&tv, NULL);
-                               if (last.tv_sec || last.tv_usec) {
-                                       /* We should wait at least a frame length */
-                                       us = sizeof(myf.frdata) / 16 * 1000;
-                                       /* Subtract 1,000,000 us for each second late we've passed */
-                                       us -= (tv.tv_sec - last.tv_sec) * 1000000;
-                                       /* And one for each us late we've passed */
-                                       us -= (tv.tv_usec - last.tv_usec);
-                                       /* Sleep that long if needed */
-                                       if (us > 0)
-                                               usleep(us);
+                       }
+                       if (f->frametype == AST_FRAME_VOICE) {
+                               /* Treat as a generator */
+                               needed = f->samples * 2;
+                               if (needed > sizeof(myf.frdata)) {
+                                       ast_log(LOG_WARNING, "Only able to deliver %d of %d requested samples\n",
+                                               (int)sizeof(myf.frdata) / 2, needed/2);
+                                       needed = sizeof(myf.frdata);
                                }
                                }
-                               last = tv;
-                               res = read(fds[0], myf.frdata, sizeof(myf.frdata));
+                               res = read(fds[0], myf.frdata, needed);
                                if (res > 0) {
                                        myf.f.frametype = AST_FRAME_VOICE;
                                        myf.f.subclass = AST_FORMAT_SLINEAR;
                                        myf.f.datalen = res;
                                        myf.f.samples = res / 2;
                                if (res > 0) {
                                        myf.f.frametype = AST_FRAME_VOICE;
                                        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;
                                        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;
                                        }
                                                break;
                                        }
-                                       if (res < sizeof(myf.frdata)) { // last frame
-                                               ast_log(LOG_WARNING, "Last frame\n");
+                                       if (res < needed) { /* last frame */
+                                               ast_debug(1, "Last frame\n");
                                                res=0;
                                                res=0;
+                                               ast_frfree(f);
                                                break;
                                        }
                                } else {
                                                break;
                                        }
                                } else {
-                                       ast_log(LOG_WARNING, "No more waveform\n");
+                                       ast_debug(1, "No more waveform\n");
                                        res = 0;
                                }
                                        res = 0;
                                }
-                       } else {
-                               ast_log(LOG_DEBUG, "HuhHHH?\n");
-                               res = -1;
-                               break;
                        }
                        }
+                       ast_frfree(f);
                }
        }
        close(fds[0]);
        close(fds[1]);
                }
        }
        close(fds[0]);
        close(fds[1]);
-//     if (pid > -1)
-//             kill(pid, SIGKILL);
+
+/*     if (pid > -1) */
+/*             kill(pid, SIGKILL); */
        if (!res && owriteformat)
                ast_set_write_format(chan, owriteformat);
        return res;
        if (!res && owriteformat)
                ast_set_write_format(chan, owriteformat);
        return res;
@@ -253,19 +280,20 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
 
 
 
 
 
 
-static int festival_exec(struct ast_channel *chan, void *data)
+static int festival_exec(struct ast_channel *chan, void *vdata)
 {
        int usecache;
        int res=0;
 {
        int usecache;
        int res=0;
-       struct localuser *u;
+       struct ast_module_user *u;
        struct sockaddr_in serv_addr;
        struct hostent *serverhost;
        struct sockaddr_in serv_addr;
        struct hostent *serverhost;
+       struct ast_hostent ahp;
        int fd;
        FILE *fs;
        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];
        int port=1314;
        int n;
        char ack[4];
@@ -276,20 +304,31 @@ static int festival_exec(struct ast_channel *chan, void *data)
        int i;
        struct MD5Context md5ctx;
        unsigned char MD5Res[16];
        int i;
        struct MD5Context md5ctx;
        unsigned char MD5Res[16];
-       char MD5Hex[32];
-       char koko[4];
-       char cachefile[MAXFESTLEN];
+       char MD5Hex[33] = "";
+       char koko[4] = "";
+       char cachefile[MAXFESTLEN]="";
        int readcache=0;
        int writecache=0;
        int strln;
        int readcache=0;
        int writecache=0;
        int strln;
-       int fdesc;
+       int fdesc = -1;
        char buffer[16384];
        char buffer[16384];
-       int seekpos;    
-       
+       int seekpos = 0;        
+       char *data;     
+       char *intstr;
        struct ast_config *cfg;
        struct ast_config *cfg;
-       cfg = ast_load(FESTIVAL_CONFIG);
+       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);
        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"))) {
                return -1;
        }
        if (!(host = ast_variable_retrieve(cfg, "general", "host"))) {
@@ -310,39 +349,68 @@ static int festival_exec(struct ast_channel *chan, void *data)
        }
        if (!(festivalcommand = ast_variable_retrieve(cfg, "general", "festivalcommand"))) {
                festivalcommand = "(tts_textasterisk \"%s\" 'file)(quit)\n";
        }
        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 (!data) {
-               ast_log(LOG_WARNING, "festival requires an argument (text)\n");
-               return -1;
+       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_WARNING, "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 */
        
        /* 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_log(LOG_WARNING,"festival_client: can't get socket\n");
-               return -1;
+               ast_config_destroy(cfg);
+               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 = gethostbyname(host);
-               if (serverhost == (struct hostent *)0) {
-                       ast_log(LOG_WARNING,"festival_client: gethostbyname failed\n");
-                       return -1;
-               }
-               memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
-       }
+
+       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);
+                       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");
        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;
        }
        
                return -1;
        }
        
@@ -350,37 +418,38 @@ static int festival_exec(struct ast_channel *chan, void *data)
        MD5Init(&md5ctx);
        MD5Update(&md5ctx,(unsigned char const *)data,strlen(data));
        MD5Final(MD5Res,&md5ctx);
        MD5Init(&md5ctx);
        MD5Update(&md5ctx,(unsigned char const *)data,strlen(data));
        MD5Final(MD5Res,&md5ctx);
-       strcpy(MD5Hex,"");
+               MD5Hex[0] = '\0';
        
        /* Convert to HEX and look if there is any matching file in the cache 
                directory */
        for (i=0;i<16;i++) {
        
        /* Convert to HEX and look if there is any matching file in the cache 
                directory */
        for (i=0;i<16;i++) {
-               sprintf(koko,"%X",MD5Res[i]);
-               strcat(MD5Hex,koko);
+               snprintf(koko, sizeof(koko), "%X",MD5Res[i]);
+               strncat(MD5Hex, koko, sizeof(MD5Hex) - strlen(MD5Hex) - 1);
        }
        readcache=0;
        writecache=0;
        }
        readcache=0;
        writecache=0;
-       if (strlen(cachedir)+strlen(MD5Hex)+1<=MAXFESTLEN && (usecache==1)) {
-               sprintf(cachefile,"%s/%s",cachedir,MD5Hex);
+       if (strlen(cachedir)+strlen(MD5Hex)+1<=MAXFESTLEN && (usecache==-1)) {
+               snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5Hex);
                fdesc=open(cachefile,O_RDWR);
                if (fdesc==-1) {
                fdesc=open(cachefile,O_RDWR);
                if (fdesc==-1) {
-                       fdesc=open(cachefile,O_CREAT|O_RDWR,0);
+                       fdesc=open(cachefile,O_CREAT|O_RDWR,AST_FILE_MODE);
                        if (fdesc!=-1) {
                                writecache=1;
                                strln=strlen((char *)data);
                        if (fdesc!=-1) {
                                writecache=1;
                                strln=strlen((char *)data);
-                               ast_log(LOG_WARNING,"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);
                                write(fdesc,&strln,sizeof(int));
                                write(fdesc,data,strln);
                                seekpos=lseek(fdesc,0,SEEK_CUR);
-                               ast_log(LOG_WARNING,"Seek position : %d\n",seekpos);
+                               ast_debug(1,"Seek position : %d\n",seekpos);
                        }
                } else {
                        read(fdesc,&strln,sizeof(int));
                        }
                } else {
                        read(fdesc,&strln,sizeof(int));
-                       ast_log(LOG_WARNING,"Cache file exists, strln=%d, strlen=%d\n",strln,strlen((char *)data));
+                       ast_debug(1,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
                        if (strlen((char *)data)==strln) {
                        if (strlen((char *)data)==strln) {
-                               ast_log(LOG_WARNING,"Size OK\n");
+                               ast_debug(1,"Size OK\n");
                                read(fdesc,&bigstring,strln);
                                read(fdesc,&bigstring,strln);
-                               if (strcmp(bigstring,data)==0) { 
+                               bigstring[strln] = 0;
+                               if (strcmp(bigstring,data)==0) { 
                                        readcache=1;
                                } else {
                                        ast_log(LOG_WARNING,"Strings do not match\n");
                                        readcache=1;
                                } else {
                                        ast_log(LOG_WARNING,"Strings do not match\n");
@@ -394,10 +463,10 @@ static int festival_exec(struct ast_channel *chan, void *data)
        if (readcache==1) {
                close(fd);
                fd=fdesc;
        if (readcache==1) {
                close(fd);
                fd=fdesc;
-               ast_log(LOG_WARNING,"Reading from cache...\n");
+               ast_debug(1,"Reading from cache...\n");
        } else {
        } else {
-               ast_log(LOG_WARNING,"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);
                fprintf(fs,festivalcommand,(char *)data);
                fflush(fs);
                fclose(fs);
@@ -405,7 +474,7 @@ static int festival_exec(struct ast_channel *chan, void *data)
        
        /* Write to cache and then pass it down */
        if (writecache==1) {
        
        /* Write to cache and then pass it down */
        if (writecache==1) {
-               ast_log(LOG_WARNING,"Writing result to cache...\n");
+               ast_debug(1,"Writing result to cache...\n");
                while ((strln=read(fd,buffer,16384))!=0) {
                        write(fdesc,buffer,strln);
                }
                while ((strln=read(fd,buffer,16384))!=0) {
                        write(fdesc,buffer,strln);
                }
@@ -415,29 +484,43 @@ static int festival_exec(struct ast_channel *chan, void *data)
                lseek(fd,seekpos,SEEK_SET);
        }
        
                lseek(fd,seekpos,SEEK_SET);
        }
        
-       ast_log(LOG_WARNING,"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 {
        
        /* 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; )
                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 */
                ack[3] = '\0';
                if (strcmp(ack,"WV\n") == 0) {         /* receive a waveform */
-                       ast_log(LOG_WARNING,"Festival WV command");
+                       ast_debug(1,"Festival WV command\n");
                        waveform = socket_receive_file_to_buff(fd,&filesize);
                        waveform = socket_receive_file_to_buff(fd,&filesize);
-                       send_waveform_to_channel(chan,waveform,filesize);
-                       free(waveform);
-                       res=0;
+                       res = send_waveform_to_channel(chan,waveform,filesize, intstr);
+                       ast_free(waveform);
                        break;
                }
                else if (strcmp(ack,"LP\n") == 0) {   /* receive an s-expr */
                        break;
                }
                else if (strcmp(ack,"LP\n") == 0) {   /* receive an s-expr */
-                       ast_log(LOG_WARNING,"Festival LP command");
+                       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);
                        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;
                } else if (strcmp(ack,"ER\n") == 0) {    /* server got an error */
                        ast_log(LOG_WARNING,"Festival returned ER\n");
                        res=-1;
@@ -445,36 +528,32 @@ static int festival_exec(struct ast_channel *chan, void *data)
                }
        } while (strcmp(ack,"OK\n") != 0);
        close(fd);
                }
        } while (strcmp(ack,"OK\n") != 0);
        close(fd);
-       LOCAL_USER_REMOVE(u);                                                                                
+       ast_config_destroy(cfg);
+       ast_module_user_remove(u);
        return res;
 
 }
 
        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;
 }
 
        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");