strescape.c 7.0 KB

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