strescape.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Functions for escaping and unescaping strings
  3. Copyright (C) 2009, 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009;
  7. Patrick Winnertz <winnie@debian.org>, 2009
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include "lib/strescape.h"
  22. /*** global variables ****************************************************************************/
  23. /*** file scope macro definitions ****************************************************************/
  24. /*** file scope type declarations ****************************************************************/
  25. /*** file scope variables ************************************************************************/
  26. static const char ESCAPE_SHELL_CHARS[] = " !#$%()&{}[]`?|<>;*\\\"'";
  27. static const char ESCAPE_REGEX_CHARS[] = "^!#$%()&{}[]`?|<>;*.\\";
  28. static const char ESCAPE_GLOB_CHARS[] = "$*\\?";
  29. /*** file scope functions ************************************************************************/
  30. /*** public functions ****************************************************************************/
  31. char *
  32. strutils_escape (const char *src, gsize src_len, const char *escaped_chars,
  33. gboolean escape_non_printable)
  34. {
  35. GString *ret;
  36. gsize curr_index;
  37. /* do NOT break allocation semantics */
  38. if (src == NULL)
  39. return NULL;
  40. if (*src == '\0')
  41. return strdup ("");
  42. ret = g_string_new ("");
  43. if (src_len == (gsize) - 1)
  44. src_len = strlen (src);
  45. for (curr_index = 0; curr_index < src_len; curr_index++)
  46. {
  47. if (escape_non_printable)
  48. {
  49. switch (src[curr_index])
  50. {
  51. case '\n':
  52. g_string_append (ret, "\\n");
  53. continue;
  54. case '\t':
  55. g_string_append (ret, "\\t");
  56. continue;
  57. case '\b':
  58. g_string_append (ret, "\\b");
  59. continue;
  60. case '\0':
  61. g_string_append (ret, "\\0");
  62. continue;
  63. }
  64. }
  65. if (strchr (escaped_chars, (int) src[curr_index]))
  66. g_string_append_c (ret, '\\');
  67. g_string_append_c (ret, src[curr_index]);
  68. }
  69. return g_string_free (ret, FALSE);
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. char *
  73. strutils_unescape (const char *src, gsize src_len, const char *unescaped_chars,
  74. gboolean unescape_non_printable)
  75. {
  76. GString *ret;
  77. gsize curr_index;
  78. if (src == NULL)
  79. return NULL;
  80. if (*src == '\0')
  81. return strdup ("");
  82. ret = g_string_sized_new (16);
  83. if (src_len == (gsize) (-1))
  84. src_len = strlen (src);
  85. src_len--;
  86. for (curr_index = 0; curr_index < src_len; curr_index++)
  87. {
  88. if (src[curr_index] != '\\')
  89. {
  90. g_string_append_c (ret, src[curr_index]);
  91. continue;
  92. }
  93. curr_index++;
  94. if (unescaped_chars == ESCAPE_SHELL_CHARS && src[curr_index] == '$')
  95. {
  96. /* special case: \$ is used to disallow variable substitution */
  97. g_string_append_c (ret, '\\');
  98. }
  99. else
  100. {
  101. if (unescape_non_printable)
  102. {
  103. switch (src[curr_index])
  104. {
  105. case 'n':
  106. g_string_append_c (ret, '\n');
  107. continue;
  108. case 't':
  109. g_string_append_c (ret, '\t');
  110. continue;
  111. case 'b':
  112. g_string_append_c (ret, '\b');
  113. continue;
  114. case '0':
  115. g_string_append_c (ret, '\0');
  116. continue;
  117. }
  118. }
  119. if (strchr (unescaped_chars, (int) src[curr_index]) == NULL)
  120. g_string_append_c (ret, '\\');
  121. }
  122. g_string_append_c (ret, src[curr_index]);
  123. }
  124. g_string_append_c (ret, src[curr_index]);
  125. return g_string_free (ret, FALSE);
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. /** To be compatible with the general posix command lines we have to escape
  129. strings for the command line
  130. \param src
  131. string for escaping
  132. \returns
  133. return escaped string (which needs to be freed later)
  134. or NULL when NULL string is passed.
  135. */
  136. char *
  137. strutils_shell_escape (const char *src)
  138. {
  139. return strutils_escape (src, -1, ESCAPE_SHELL_CHARS, FALSE);
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. char *
  143. strutils_glob_escape (const char *src)
  144. {
  145. return strutils_escape (src, -1, ESCAPE_GLOB_CHARS, TRUE);
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. char *
  149. strutils_regex_escape (const char *src)
  150. {
  151. return strutils_escape (src, -1, ESCAPE_REGEX_CHARS, TRUE);
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. /** Unescape paths or other strings for e.g the internal cd
  155. shell-unescape within a given buffer (writing to it!)
  156. \param text
  157. string for unescaping
  158. \returns
  159. return unescaped string (which needs to be freed)
  160. */
  161. char *
  162. strutils_shell_unescape (const char *text)
  163. {
  164. return strutils_unescape (text, -1, ESCAPE_SHELL_CHARS, TRUE);
  165. }
  166. /* --------------------------------------------------------------------------------------------- */
  167. char *
  168. strutils_glob_unescape (const char *text)
  169. {
  170. return strutils_unescape (text, -1, ESCAPE_GLOB_CHARS, TRUE);
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. char *
  174. strutils_regex_unescape (const char *text)
  175. {
  176. return strutils_unescape (text, -1, ESCAPE_REGEX_CHARS, TRUE);
  177. }
  178. /* --------------------------------------------------------------------------------------------- */
  179. /** Check if char in pointer contain escape'd chars
  180. \param start
  181. string for checking
  182. \param current
  183. pointer to checked character
  184. \returns
  185. return TRUE if string contain escaped chars
  186. otherwise return FALSE
  187. */
  188. gboolean
  189. strutils_is_char_escaped (const char *start, const char *current)
  190. {
  191. int num_esc = 0;
  192. if (start == NULL || current == NULL || current <= start)
  193. return FALSE;
  194. current--;
  195. while (current >= start && *current == '\\')
  196. {
  197. num_esc++;
  198. current--;
  199. }
  200. return (gboolean) num_esc % 2;
  201. }
  202. /* --------------------------------------------------------------------------------------------- */