adds a pool based implementation of the switch_util_quote_shell_arg function and changes existing function to be a wrapper around the pool based one
/*!
\brief Quote shell argument
\param string the string to quote (example: a ' b"' c)
- \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows)
+ \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), should be freed
*/
SWITCH_DECLARE(char *) switch_util_quote_shell_arg(const char *string);
+/*!
+ \brief Quote shell argument, allocating from pool if provided
+ \param string the string to quote (example: a ' b"' c)
+ \param pool a memory pool to use
+ \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), if pool not provided, returned value should be freed
+*/
+SWITCH_DECLARE(char *) switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool);
+
+
#define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK)
SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len);
SWITCH_DECLARE(char *) switch_url_decode(char *s);
return SWITCH_STATUS_SUCCESS;
}
+SWITCH_STANDARD_API(quote_shell_arg_function)
+{
+ switch_memory_pool_t *pool;
+
+ if (zstr(cmd)) {
+ return SWITCH_STATUS_SUCCESS;
+ }
+
+ switch_core_new_memory_pool(&pool);
+
+ stream->write_function(stream, "%s", switch_util_quote_shell_arg_pool(cmd, pool));
+
+ switch_core_destroy_memory_pool(&pool);
+ return SWITCH_STATUS_SUCCESS;
+}
+
#define UUID_LOGLEVEL_SYNTAX "<uuid> <level>"
SWITCH_STANDARD_API(uuid_loglevel)
{
SWITCH_ADD_API(commands_api_interface, "nat_map", "Manage NAT", nat_map_function, "[status|republish|reinit] | [add|del] <port> [tcp|udp] [static]");
SWITCH_ADD_API(commands_api_interface, "originate", "Originate a call", originate_function, ORIGINATE_SYNTAX);
SWITCH_ADD_API(commands_api_interface, "pause", "Pause media on a channel", pause_function, PAUSE_SYNTAX);
+ SWITCH_ADD_API(commands_api_interface, "quote_shell_arg", "Quote/escape a string for use on shell command line", quote_shell_arg_function, "<data>");
SWITCH_ADD_API(commands_api_interface, "regex", "Evaluate a regex", regex_function, "<data>|<pattern>[|<subst string>]");
SWITCH_ADD_API(commands_api_interface, "reloadacl", "Reload XML", reload_acl_function, "");
SWITCH_ADD_API(commands_api_interface, "reload", "Reload module", reload_function, UNLOAD_SYNTAX);
}
SWITCH_DECLARE(char *) switch_util_quote_shell_arg(const char *string)
+{
+ return switch_util_quote_shell_arg_pool(string, NULL);
+}
+
+SWITCH_DECLARE(char *) switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool)
{
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;
+ size_t dest_len = 0;
+ char *dest;
- dest = (char *) malloc(sizeof(char) * dest_len);
+ /* first pass through, figure out how large to make the allocation */
+ dest_len = strlen(string) + 1; /* string + null */
+ dest_len += 1; /* opening quote */
+ for (i = 0; i < string_len; i++) {
+ switch (string[i]) {
+#ifndef WIN32
+ case '\'':
+ /* We replace ' by sq dq sq dq sq, so need 4 additional bytes */
+ dest_len += 4;
+ break;
+#endif
+ }
+ }
+ dest_len += 1; /* closing quote */
+
+ /* if we're given a pool, allocate from it, otherwise use malloc */
+ if ( pool ) {
+ dest = switch_core_alloc(pool, sizeof(char) * dest_len);
+ } else {
+ dest = (char *) malloc(sizeof(char) * dest_len);
+ }
switch_assert(dest);
#ifdef WIN32
break;
#else
case '\'':
- /* We replace ' by '\'' */
- dest_len += 3;
- tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
- switch_assert(tmp);
- dest = tmp;
+ /* We replace ' by sq dq sq dq sq */
dest[n++] = '\'';
- dest[n++] = '\\';
+ dest[n++] = '"';
dest[n++] = '\'';
+ dest[n++] = '"';
dest[n++] = '\'';
break;
#endif
}
}
- dest_len += 2; /* +2 for the closing quote and the null character */
- tmp = (char *) realloc(dest, sizeof(char) * (dest_len));
- switch_assert(tmp);
- dest = tmp;
#ifdef WIN32
dest[n++] = '"';
#else