mod_tts_commandline: quote shell args, create an unique tempfile

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14931 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Mathieu Parent 2009-09-21 08:48:12 +00:00
parent b7b88fabfc
commit 1d028b5437

View File

@ -32,6 +32,8 @@
#include <unistd.h>
#include <switch.h>
SWITCH_DECLARE(char *) switch_escape_shell_arg(char *string);
SWITCH_MODULE_LOAD_FUNCTION(mod_tts_commandline_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_tts_commandline_shutdown);
SWITCH_MODULE_DEFINITION(mod_tts_commandline, mod_tts_commandline_load, mod_tts_commandline_shutdown, NULL);
@ -48,11 +50,68 @@ struct tts_commandline_data {
switch_file_handle_t fh;
char *voice_name;
int rate;
char file[512];
char *file;
};
typedef struct tts_commandline_data tts_commandline_t;
SWITCH_DECLARE(char *) switch_quote_shell_arg(char *string)
{
size_t string_len = strlen(string);
size_t i;
size_t n = 0;
size_t dest_len = string_len + 1; /* +1 for the opening quote */
char *dest, *tmp;
dest = (char *) malloc(sizeof(char) * dest_len);
switch_assert(dest);
#ifdef WIN32
dest[n++] = '"';
#else
dest[n++] = '\'';
#endif
for (i = 0; i < string_len; i++) {
switch (string[i]) {
#ifdef WIN32
case '"':
case '%':
dest[n++] = ' ';
break;
#else
case '\'':
/* We replace ' by '\'' */
dest_len+=3;
tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
switch_assert(tmp);
dest = tmp;
dest[n++] = '\'';
dest[n++] = '\\';
dest[n++] = '\'';
dest[n++] = '\'';
break;
#endif
default:
dest[n++] = string[i];
}
}
dest_len++;
tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
switch_assert(tmp);
dest = tmp;
#ifdef WIN32
dest[n++] = '"';
#else
dest[n++] = '\'';
#endif
dest[dest_len] = 0;
return dest;
}
static int load_tts_commandline_config(void)
{
char *cf = "tts_commandline.conf";
@ -92,12 +151,20 @@ static void event_handler(switch_event_t *event)
static switch_status_t tts_commandline_speech_open(switch_speech_handle_t *sh, const char *voice_name, int rate, switch_speech_flag_t *flags)
{
switch_uuid_t uuid;
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
char outfile[512] = "";
tts_commandline_t *info = switch_core_alloc(sh->memory_pool, sizeof(*info));
info->voice_name = switch_core_strdup(sh->memory_pool, voice_name);
info->rate = rate;
switch_snprintf(info->file, sizeof(info->file), "%s%s.wav", SWITCH_GLOBAL_dirs.temp_dir, "tts_commandline");
switch_uuid_get(&uuid);
switch_uuid_format(uuid_str, &uuid);
switch_snprintf(outfile, sizeof(outfile), "%s%s.tmp.wav", SWITCH_GLOBAL_dirs.temp_dir, uuid_str);
info->file = outfile;
sh->private_info = info;
@ -117,14 +184,22 @@ static switch_status_t tts_commandline_speech_feed_tts(switch_speech_handle_t *s
tts_commandline_t *info = (tts_commandline_t *) sh->private_info;
assert(info != NULL);
char *message;
char *rate;
char *message, *tmp, *rate;
message = switch_core_strdup(sh->memory_pool, globals.command);
message = switch_string_replace(message, "${text}", text);
message = switch_string_replace(message, "${voice}", info->voice_name);
tmp = switch_quote_shell_arg(text);
message = switch_string_replace(message, "${text}", tmp);
tmp = switch_quote_shell_arg(info->voice_name);
message = switch_string_replace(message, "${voice}", tmp);
rate = switch_core_sprintf(sh->memory_pool, "%d", info->rate);
message = switch_string_replace(message, "${rate}", rate);
message = switch_string_replace(message, "${file}", info->file);
tmp = switch_quote_shell_arg(info->file);
message = switch_string_replace(message, "${file}", tmp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Executing: %s\n", message);
if (switch_system(message, SWITCH_TRUE) < 0) {