Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / apps / app_festival.c
index dafd894..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,8 +221,7 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                break;
                        }
                        if (f->frametype == AST_FRAME_DTMF) {
-                               if (option_debug)
-                                       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);
@@ -232,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;
@@ -242,15 +251,13 @@ static int send_waveform_to_channel(struct ast_channel *chan, char *waveform, in
                                                break;
                                        }
                                        if (res < needed) { /* last frame */
-                                               if (option_debug)
-                                                       ast_log(LOG_DEBUG, "Last frame\n");
+                                               ast_debug(1, "Last frame\n");
                                                res=0;
                                                ast_frfree(f);
                                                break;
                                        }
                                } else {
-                                       if (option_debug)
-                                               ast_log(LOG_DEBUG, "No more waveform\n");
+                                       ast_debug(1, "No more waveform\n");
                                        res = 0;
                                }
                        }
@@ -370,8 +377,7 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                        intstr = AST_DIGIT_ANY;
        }
        
-       if (option_debug)
-               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);
@@ -426,25 +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);
-                               if (option_debug)
-                                       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);
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG,"Seek position : %d\n",seekpos);
+                               ast_debug(1,"Seek position : %d\n",seekpos);
                        }
                } else {
                        read(fdesc,&strln,sizeof(int));
-                       if (option_debug)
-                               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) {
-                               if (option_debug)
-                                       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) { 
@@ -461,11 +463,9 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        if (readcache==1) {
                close(fd);
                fd=fdesc;
-               if (option_debug)
-                       ast_log(LOG_DEBUG,"Reading from cache...\n");
+               ast_debug(1,"Reading from cache...\n");
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG,"Passing text to festival...\n");
+               ast_debug(1,"Passing text to festival...\n");
                fs=fdopen(dup(fd),"wb");
                fprintf(fs,festivalcommand,(char *)data);
                fflush(fs);
@@ -474,8 +474,7 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
        
        /* Write to cache and then pass it down */
        if (writecache==1) {
-               if (option_debug)
-                       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);
                }
@@ -485,8 +484,7 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                lseek(fd,seekpos,SEEK_SET);
        }
        
-       if (option_debug)
-               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 */
@@ -511,20 +509,18 @@ static int festival_exec(struct ast_channel *chan, void *vdata)
                }
                ack[3] = '\0';
                if (strcmp(ack,"WV\n") == 0) {         /* receive a waveform */
-                       if (option_debug)
-                               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 */
-                       if (option_debug)
-                               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;