2009-06-09 12:50:45 +04:00
|
|
|
/*
|
|
|
|
Functions for escaping and unescaping strings
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2009-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2009-06-09 12:50:45 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009;
|
|
|
|
Patrick Winnertz <winnie@debian.org>, 2009
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2009-06-09 12:50:45 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-06-09 12:50:45 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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.
|
2009-06-09 12:50:45 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-09 12:50:45 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-04-09 13:56:43 +04:00
|
|
|
#include "lib/global.h"
|
2024-03-09 14:41:08 +03:00
|
|
|
#include "lib/strutil.h"
|
2009-06-09 12:50:45 +04:00
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2009-12-25 21:42:23 +03:00
|
|
|
static const char ESCAPE_SHELL_CHARS[] = " !#$%()&{}[]`?|<>;*\\\"'";
|
2021-02-07 16:47:14 +03:00
|
|
|
static const char ESCAPE_REGEX_CHARS[] = "^!#$%()&{}[]`?|<>;*+.\\";
|
2010-07-07 15:04:38 +04:00
|
|
|
static const char ESCAPE_GLOB_CHARS[] = "$*\\?";
|
2009-06-09 16:43:50 +04:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 12:50:45 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 12:50:45 +04:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 12:50:45 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-09 12:50:45 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_escape (const char *src, gsize src_len, const char *escaped_chars,
|
|
|
|
gboolean escape_non_printable)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
|
|
|
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 ("");
|
|
|
|
|
2012-11-25 11:44:15 +04:00
|
|
|
if (src_len == (gsize) (-1))
|
2009-06-09 16:14:11 +04:00
|
|
|
src_len = strlen (src);
|
|
|
|
|
2010-07-07 15:04:38 +04:00
|
|
|
for (curr_index = 0; curr_index < src_len; curr_index++)
|
|
|
|
{
|
|
|
|
if (escape_non_printable)
|
|
|
|
{
|
|
|
|
switch (src[curr_index])
|
|
|
|
{
|
2009-06-09 16:14:11 +04:00
|
|
|
case '\n':
|
|
|
|
g_string_append (ret, "\\n");
|
|
|
|
continue;
|
|
|
|
case '\t':
|
|
|
|
g_string_append (ret, "\\t");
|
|
|
|
continue;
|
|
|
|
case '\b':
|
|
|
|
g_string_append (ret, "\\b");
|
|
|
|
continue;
|
|
|
|
case '\0':
|
|
|
|
g_string_append (ret, "\\0");
|
|
|
|
continue;
|
2015-04-19 13:57:38 +03:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
char *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_unescape (const char *src, gsize src_len, const char *unescaped_chars,
|
|
|
|
gboolean unescape_non_printable)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
|
|
|
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 ("");
|
|
|
|
|
2011-10-12 14:04:05 +04:00
|
|
|
ret = g_string_sized_new (16);
|
2009-06-09 16:14:11 +04:00
|
|
|
|
2011-10-12 14:04:05 +04:00
|
|
|
if (src_len == (gsize) (-1))
|
2009-06-09 16:14:11 +04:00
|
|
|
src_len = strlen (src);
|
2011-10-12 14:04:05 +04:00
|
|
|
src_len--;
|
2009-06-09 16:14:11 +04:00
|
|
|
|
2011-10-12 14:04:05 +04:00
|
|
|
for (curr_index = 0; curr_index < src_len; curr_index++)
|
2010-07-07 15:04:38 +04:00
|
|
|
{
|
|
|
|
if (src[curr_index] != '\\')
|
|
|
|
{
|
2009-06-09 17:08:48 +04:00
|
|
|
g_string_append_c (ret, src[curr_index]);
|
2009-06-09 16:14:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-10-12 14:04:05 +04:00
|
|
|
|
2009-06-09 17:08:48 +04:00
|
|
|
curr_index++;
|
2011-10-12 14:04:05 +04:00
|
|
|
|
|
|
|
if (unescaped_chars == ESCAPE_SHELL_CHARS && src[curr_index] == '$')
|
2010-07-07 15:04:38 +04:00
|
|
|
{
|
2011-10-12 14:04:05 +04:00
|
|
|
/* special case: \$ is used to disallow variable substitution */
|
|
|
|
g_string_append_c (ret, '\\');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unescape_non_printable)
|
2010-07-07 15:04:38 +04:00
|
|
|
{
|
2011-10-12 14:04:05 +04:00
|
|
|
switch (src[curr_index])
|
|
|
|
{
|
|
|
|
case 'n':
|
|
|
|
g_string_append_c (ret, '\n');
|
|
|
|
continue;
|
|
|
|
case 't':
|
|
|
|
g_string_append_c (ret, '\t');
|
|
|
|
continue;
|
|
|
|
case 'b':
|
|
|
|
g_string_append_c (ret, '\b');
|
|
|
|
continue;
|
|
|
|
case '0':
|
|
|
|
g_string_append_c (ret, '\0');
|
|
|
|
continue;
|
2015-04-19 13:57:38 +03:00
|
|
|
default:
|
|
|
|
break;
|
2011-10-12 14:04:05 +04:00
|
|
|
}
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
2011-10-12 14:04:05 +04:00
|
|
|
|
|
|
|
if (strchr (unescaped_chars, (int) src[curr_index]) == NULL)
|
|
|
|
g_string_append_c (ret, '\\');
|
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
|
|
|
}
|
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);
|
|
|
|
}
|
2010-07-07 15:04:38 +04:00
|
|
|
|
2009-06-09 16:14:11 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2012-08-31 18:05:29 +04:00
|
|
|
/**
|
|
|
|
* To be compatible with the general posix command lines we have to escape
|
|
|
|
* strings for the command line
|
|
|
|
*
|
|
|
|
* @param src string for escaping
|
|
|
|
*
|
2012-11-05 17:57:50 +04:00
|
|
|
* @return escaped string (which needs to be freed later) or NULL when NULL string is passed.
|
2009-06-09 12:50:45 +04:00
|
|
|
*/
|
2012-08-31 18:05:29 +04:00
|
|
|
|
2009-06-09 13:06:45 +04:00
|
|
|
char *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_shell_escape (const char *src)
|
2009-06-09 12:50:45 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_escape (src, -1, ESCAPE_SHELL_CHARS, FALSE);
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
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 *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_glob_escape (const char *src)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_escape (src, -1, ESCAPE_GLOB_CHARS, TRUE);
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
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 *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_regex_escape (const char *src)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_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
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2012-08-31 18:05:29 +04:00
|
|
|
/**
|
|
|
|
* Unescape paths or other strings for e.g the internal cd
|
|
|
|
* shell-unescape within a given buffer (writing to it!)
|
|
|
|
*
|
|
|
|
* @param text string for unescaping
|
|
|
|
*
|
2012-11-05 17:57:50 +04:00
|
|
|
* @return unescaped string (which needs to be freed)
|
2009-06-09 12:50:45 +04:00
|
|
|
*/
|
2012-08-31 18:05:29 +04:00
|
|
|
|
2009-06-09 13:06:45 +04:00
|
|
|
char *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_shell_unescape (const char *text)
|
2009-06-09 12:50:45 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_unescape (text, -1, ESCAPE_SHELL_CHARS, TRUE);
|
2009-06-09 16:14:11 +04:00
|
|
|
}
|
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 *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_glob_unescape (const char *text)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_unescape (text, -1, ESCAPE_GLOB_CHARS, TRUE);
|
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 *
|
2024-03-09 14:41:08 +03:00
|
|
|
str_regex_unescape (const char *text)
|
2009-06-09 16:14:11 +04:00
|
|
|
{
|
2024-03-09 14:41:08 +03:00
|
|
|
return str_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
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2012-08-31 18:05:29 +04:00
|
|
|
/**
|
|
|
|
* Check if char in pointer contain escape'd chars
|
|
|
|
*
|
|
|
|
* @param start string for checking
|
|
|
|
* @param current pointer to checked character
|
|
|
|
*
|
2012-11-05 17:57:50 +04:00
|
|
|
* @return TRUE if string contain escaped chars otherwise return FALSE
|
2009-06-09 12:50:45 +04:00
|
|
|
*/
|
2012-08-31 18:05:29 +04:00
|
|
|
|
2009-06-09 12:50:45 +04:00
|
|
|
gboolean
|
2024-03-09 14:41:08 +03:00
|
|
|
str_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--;
|
2010-07-07 15:04:38 +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
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|