strescape.c 6.9 KB

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