strescape.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_new ("");
  83. if (src_len == (gsize) - 1)
  84. src_len = strlen (src);
  85. for (curr_index = 0; curr_index < src_len - 1; curr_index++)
  86. {
  87. if (src[curr_index] != '\\')
  88. {
  89. g_string_append_c (ret, src[curr_index]);
  90. continue;
  91. }
  92. curr_index++;
  93. if (unescape_non_printable)
  94. {
  95. switch (src[curr_index])
  96. {
  97. case 'n':
  98. g_string_append_c (ret, '\n');
  99. continue;
  100. case 't':
  101. g_string_append_c (ret, '\t');
  102. continue;
  103. case 'b':
  104. g_string_append_c (ret, '\b');
  105. continue;
  106. case '0':
  107. g_string_append (ret, '\0');
  108. continue;
  109. }
  110. }
  111. if (strchr (unescaped_chars, (int) src[curr_index]) == NULL)
  112. g_string_append_c (ret, '\\');
  113. g_string_append_c (ret, src[curr_index]);
  114. }
  115. g_string_append_c (ret, src[curr_index]);
  116. return g_string_free (ret, FALSE);
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. /** To be compatible with the general posix command lines we have to escape
  120. strings for the command line
  121. \param src
  122. string for escaping
  123. \returns
  124. return escaped string (which needs to be freed later)
  125. or NULL when NULL string is passed.
  126. */
  127. char *
  128. strutils_shell_escape (const char *src)
  129. {
  130. return strutils_escape (src, -1, ESCAPE_SHELL_CHARS, FALSE);
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. char *
  134. strutils_glob_escape (const char *src)
  135. {
  136. return strutils_escape (src, -1, ESCAPE_GLOB_CHARS, TRUE);
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. char *
  140. strutils_regex_escape (const char *src)
  141. {
  142. return strutils_escape (src, -1, ESCAPE_REGEX_CHARS, TRUE);
  143. }
  144. /* --------------------------------------------------------------------------------------------- */
  145. /** Unescape paths or other strings for e.g the internal cd
  146. shell-unescape within a given buffer (writing to it!)
  147. \param text
  148. string for unescaping
  149. \returns
  150. return unescaped string (which needs to be freed)
  151. */
  152. char *
  153. strutils_shell_unescape (const char *text)
  154. {
  155. return strutils_unescape (text, -1, ESCAPE_SHELL_CHARS, TRUE);
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. char *
  159. strutils_glob_unescape (const char *text)
  160. {
  161. return strutils_unescape (text, -1, ESCAPE_GLOB_CHARS, TRUE);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. char *
  165. strutils_regex_unescape (const char *text)
  166. {
  167. return strutils_unescape (text, -1, ESCAPE_REGEX_CHARS, TRUE);
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. /** Check if char in pointer contain escape'd chars
  171. \param start
  172. string for checking
  173. \param current
  174. pointer to checked character
  175. \returns
  176. return TRUE if string contain escaped chars
  177. otherwise return FALSE
  178. */
  179. gboolean
  180. strutils_is_char_escaped (const char *start, const char *current)
  181. {
  182. int num_esc = 0;
  183. if (start == NULL || current == NULL || current <= start)
  184. return FALSE;
  185. current--;
  186. while (current >= start && *current == '\\')
  187. {
  188. num_esc++;
  189. current--;
  190. }
  191. return (gboolean) num_esc % 2;
  192. }
  193. /* --------------------------------------------------------------------------------------------- */