Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / apps / app_festival.c
index d365b24..6a8a26f 100644 (file)
@@ -21,6 +21,8 @@
  * \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
  */
@@ -73,7 +75,7 @@ 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;
@@ -130,19 +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)
@@ -171,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");
@@ -210,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);
@@ -231,7 +242,6 @@ 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;
@@ -241,13 +251,13 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                                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;
                                }
                        }
@@ -280,10 +290,10 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        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];
@@ -367,29 +377,33 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                        intstr = AST_DIGIT_ANY;
        }
        
-       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);
                ast_module_user_remove(u);
-               return -1;
+               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);
                        ast_module_user_remove(u);
-                       return -1;
-               }
-               memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
-       }
+                       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);
 
@@ -418,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) { 
@@ -449,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);
@@ -460,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);
                }
@@ -470,7 +484,7 @@ 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 */
@@ -485,7 +499,7 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                         * */
                        if ( read_data == -1 )
                        {
-                               ast_log(LOG_WARNING,"Unable to read from cache/festival fd");
+                               ast_log(LOG_WARNING,"Unable to read from cache/festival fd\n");
                               close(fd);
                               ast_config_destroy(cfg);
                               ast_module_user_remove(u);
@@ -495,18 +509,18 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                }
                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;