2009-06-09 12:50:45 +04:00
|
|
|
/*
|
|
|
|
Functions for escaping and unescaping strings
|
|
|
|
|
|
|
|
Copyright (C) 2009 The Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009;
|
|
|
|
Patrick Winnertz <winnie@debian.org>, 2009
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2010-01-21 16:39:17 +03:00
|
|
|
#include "lib/strescape.h"
|
2009-06-09 12:50:45 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2009-12-25 21:42:23 +03:00
|
|
|
static const char ESCAPE_SHELL_CHARS[] = " !#$%()&{}[]`?|<>;*\\\"'";
|
2009-08-20 00:10:33 +04:00
|
|
|
static const char ESCAPE_REGEX_CHARS[] = "^!#$%()&{}[]`?|<>;*.\\";
|
2009-06-22 13:04:23 +04:00
|
|
|
static const char ESCAPE_GLOB_CHARS[] = "$*\\";
|
2009-06-09 16:43:50 +04:00
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
2009-07-15 22:02:50 +04:00
|
|
|
strutils_escape (const char *src, gsize src_len, const char *escaped_chars,
|
2009-06-09 16:14:11 +04:00
|
|
|
gboolean escape_non_printable)
|
|
|
|
{
|
|
|
|
GString *ret;
|
2009-06-09 17:08:48 +04:00
|
|
|
gsize curr_index;
|
2009-06-09 16:14:11 +04:00
|
|
|
/* do NOT break allocation semantics */
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (*src == '\0')
|
|
|
|
return strdup ("");
|
|
|
|
|
|
|
|
ret = g_string_new ("");
|
|
|
|
|
2009-07-15 22:02:50 +04:00
|
|
|
if (src_len == (gsize)-1)
|
2009-06-09 16:14:11 +04:00
|
|
|
src_len = strlen (src);
|
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
for (curr_index = 0; curr_index < src_len; curr_index++) {
|
2009-06-09 16:14:11 +04:00
|
|
|
|
|
|
|
if (escape_non_printable) {
|
2009-06-09 17:08:48 +04:00
|
|
|
switch (src[curr_index]) {
|
2009-06-09 16:14:11 +04:00
|
|
|
case '\n':
|
|
|
|
g_string_append (ret, "\\n");
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
g_string_append (ret, "\\t");
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
g_string_append (ret, "\\b");
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
g_string_append (ret, "\\0");
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
if (strchr (escaped_chars, (int) src[curr_index]))
|
2009-06-09 16:14:11 +04:00
|
|
|
g_string_append_c (ret, '\\');
|
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
g_string_append_c (ret, src[curr_index]);
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
|
|
|
return g_string_free (ret, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
char *
|
2009-07-15 22:02:50 +04:00
|
|
|
strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars,
|
2009-06-09 16:14:11 +04:00
|
|
|
gboolean unescape_non_printable)
|
|
|
|
{
|
|
|
|
GString *ret;
|
2009-06-09 17:08:48 +04:00
|
|
|
gsize curr_index;
|
2009-06-09 16:14:11 +04:00
|
|
|
|
|
|
|
if (src == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (*src == '\0')
|
|
|
|
return strdup ("");
|
|
|
|
|
|
|
|
ret = g_string_new ("");
|
|
|
|
|
2009-07-15 22:02:50 +04:00
|
|
|
if (src_len == (gsize)-1)
|
2009-06-09 16:14:11 +04:00
|
|
|
src_len = strlen (src);
|
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
for (curr_index = 0; curr_index < src_len-1; curr_index++) {
|
|
|
|
if (src[curr_index] != '\\'){
|
|
|
|
g_string_append_c (ret, src[curr_index]);
|
2009-06-09 16:14:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-06-09 17:08:48 +04:00
|
|
|
curr_index++;
|
2009-06-09 16:14:11 +04:00
|
|
|
if (unescape_non_printable) {
|
2009-06-09 17:08:48 +04:00
|
|
|
switch (src[curr_index]) {
|
2009-06-09 16:14:11 +04:00
|
|
|
case 'n':
|
|
|
|
g_string_append_c (ret, '\n');
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
g_string_append_c (ret, '\t');
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
g_string_append_c (ret, '\b');
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
g_string_append (ret, '\0');
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-06-09 17:08:48 +04:00
|
|
|
if (strchr (unescaped_chars, (int) src[curr_index]) == NULL)
|
2009-06-09 16:14:11 +04:00
|
|
|
g_string_append_c (ret, '\\');
|
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
g_string_append_c (ret, src[curr_index]);
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
2009-06-09 17:08:48 +04:00
|
|
|
g_string_append_c (ret, src[curr_index]);
|
2009-06-09 16:14:11 +04:00
|
|
|
|
|
|
|
return g_string_free (ret, FALSE);
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
/** To be compatible with the general posix command lines we have to escape
|
|
|
|
strings for the command line
|
|
|
|
|
2009-12-07 20:38:59 +03:00
|
|
|
\param src
|
2009-06-09 12:50:45 +04:00
|
|
|
string for escaping
|
|
|
|
|
|
|
|
\returns
|
|
|
|
return escaped string (which needs to be freed later)
|
|
|
|
or NULL when NULL string is passed.
|
|
|
|
*/
|
2009-06-09 13:06:45 +04:00
|
|
|
char *
|
2009-06-09 13:12:17 +04:00
|
|
|
strutils_shell_escape (const char *src)
|
2009-06-09 12:50:45 +04:00
|
|
|
{
|
2009-06-09 16:14:11 +04:00
|
|
|
return strutils_escape (src, -1, ESCAPE_SHELL_CHARS, FALSE);
|
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
|
|
|
strutils_glob_escape (const char *src)
|
|
|
|
{
|
|
|
|
return strutils_escape (src, -1, ESCAPE_GLOB_CHARS, TRUE);
|
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
|
|
|
strutils_regex_escape (const char *src)
|
|
|
|
{
|
|
|
|
return strutils_escape (src, -1, ESCAPE_REGEX_CHARS, TRUE);
|
2009-06-09 12:50:45 +04:00
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/** Unescape paths or other strings for e.g the internal cd
|
|
|
|
shell-unescape within a given buffer (writing to it!)
|
|
|
|
|
2009-12-07 20:38:59 +03:00
|
|
|
\param text
|
2009-06-09 12:50:45 +04:00
|
|
|
string for unescaping
|
|
|
|
|
|
|
|
\returns
|
|
|
|
return unescaped string (which needs to be freed)
|
|
|
|
*/
|
2009-06-09 13:06:45 +04:00
|
|
|
char *
|
2009-06-09 13:12:17 +04:00
|
|
|
strutils_shell_unescape (const char *text)
|
2009-06-09 12:50:45 +04:00
|
|
|
{
|
2009-06-09 16:14:11 +04:00
|
|
|
return strutils_unescape (text, -1, ESCAPE_SHELL_CHARS, TRUE);
|
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
|
|
|
strutils_glob_unescape (const char *text)
|
|
|
|
{
|
|
|
|
return strutils_unescape (text, -1, ESCAPE_GLOB_CHARS, TRUE);
|
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
char *
|
|
|
|
strutils_regex_unescape (const char *text)
|
|
|
|
{
|
|
|
|
return strutils_unescape (text, -1, ESCAPE_REGEX_CHARS, TRUE);
|
2009-06-09 12:50:45 +04:00
|
|
|
}
|
2009-06-09 13:06:45 +04:00
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/** Check if char in pointer contain escape'd chars
|
|
|
|
|
2009-12-07 20:38:59 +03:00
|
|
|
\param start
|
2009-06-09 12:50:45 +04:00
|
|
|
string for checking
|
|
|
|
|
2009-12-07 20:38:59 +03:00
|
|
|
\param current
|
|
|
|
pointer to checked character
|
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
\returns
|
|
|
|
return TRUE if string contain escaped chars
|
|
|
|
otherwise return FALSE
|
|
|
|
*/
|
|
|
|
gboolean
|
2009-06-09 13:06:45 +04:00
|
|
|
strutils_is_char_escaped (const char *start, const char *current)
|
2009-06-09 12:50:45 +04:00
|
|
|
{
|
2009-06-09 13:01:00 +04:00
|
|
|
int num_esc = 0;
|
|
|
|
|
|
|
|
if (start == NULL || current == NULL || current <= start)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
current--;
|
2009-06-09 13:06:45 +04:00
|
|
|
while (current >= start && *current == '\\') {
|
2009-06-09 13:01:00 +04:00
|
|
|
num_esc++;
|
|
|
|
current--;
|
|
|
|
}
|
|
|
|
return (gboolean) num_esc % 2;
|
2009-06-09 12:50:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|