]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add escape api command to escape a string
authorMathieu Rene <mrene@avgs.ca>
Thu, 3 Dec 2009 02:37:05 +0000 (02:37 +0000)
committerMathieu Rene <mrene@avgs.ca>
Thu, 3 Dec 2009 02:37:05 +0000 (02:37 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15764 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_utils.h
src/mod/applications/mod_commands/mod_commands.c
src/switch_utils.c

index 8e3bebedd9241ffdc2f191f7c933409fe17f8170..e10ba5d0547051a4f063f2a24a0f508ca1aaee7a 100644 (file)
@@ -557,6 +557,9 @@ SWITCH_DECLARE(switch_bool_t) switch_ast2regex(const char *pat, char *rbuf, size
 */
 SWITCH_DECLARE(char *) switch_escape_char(switch_memory_pool_t *pool, char *in, const char *delim, char esc);
 
+SWITCH_DECLARE(char *) switch_escape_string(const char *in, char *out, switch_size_t outlen);
+SWITCH_DECLARE(char*) switch_escape_string_pool(const char *in, switch_memory_pool_t *pool);
+
 /*!
   \brief Wait for a socket
   \param poll the pollfd to wait on
index 99bc67185290d5075e638c9693a97ad1bf0274a0..8537ef87f9430a569631fab1345ade43fe2ea345 100644 (file)
@@ -3697,6 +3697,17 @@ SWITCH_STANDARD_API(hupall_api_function)
        return SWITCH_STATUS_SUCCESS;
 }
 
+SWITCH_STANDARD_API(escape_function)
+{
+       int len = strlen(cmd)*2;
+       char *mycmd = malloc(strlen(cmd)*2);
+       
+       stream->write_function(stream, "%s", switch_escape_string(cmd, mycmd, len));
+       
+       switch_safe_free(mycmd);
+       return SWITCH_STATUS_SUCCESS;
+}
+
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_commands_shutdown)
 {
        int x;
@@ -3856,6 +3867,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
        SWITCH_ADD_API(commands_api_interface, "host_lookup", "host_lookup", host_lookup_function, "<hostname>");
        SWITCH_ADD_API(commands_api_interface, "hostname", "Returns the system hostname", hostname_api_function, "");
        SWITCH_ADD_API(commands_api_interface, "db_cache", "db cache management", db_cache_function, "status");
+       SWITCH_ADD_API(commands_api_interface, "escape", "escape a string", escape_function, "<data>");
        switch_console_set_complete("db_cache status");
 
        /* indicate that the module should continue to be loaded */
index cb039e269267f5e8830bf960d411e839aee275b6..4b1524f5af547d2745c7dfdd35b64e8e36f6d5ce 100644 (file)
@@ -1577,6 +1577,51 @@ static char unescape_char(char escaped)
        return unescaped;
 }
 
+SWITCH_DECLARE(char *) switch_escape_string(const char *in, char *out, switch_size_t outlen)
+{
+       const char *p;
+       char *o = out;
+       
+       for(p = in; *p; p++) {
+               switch(*p) {
+                       case '\n':
+                               *o++ = '\\';
+                               *o++ = 'n';
+                               break;
+                       case '\r':
+                               *o++ = '\\';
+                               *o++ = 'r';
+                               break;
+                       case '\t':
+                               *o++ = '\\';
+                               *o++ = 't';
+                               break;
+                       case ' ':
+                               *o++ = '\\';
+                               *o++ = 's';
+                               break;
+                       case '$':
+                               *o++ = '\\';
+                               *o++ = '$';
+                               break;
+                       default:
+                               *o++ = *p;
+                               break;
+               }
+       }
+
+       *o++ = '\0';
+
+       return out;     
+}
+
+SWITCH_DECLARE(char*) switch_escape_string_pool(const char *in, switch_memory_pool_t *pool)
+{
+       int len = strlen(in)*2;
+       char *buf = switch_core_alloc(pool, len);
+       return switch_escape_string(in, buf, len);
+}
+
 /* Helper function used when separating strings to remove quotes, leading /
    trailing spaces, and to convert escaped characters. */
 static char *cleanup_separated_string(char *str, char delim)