4 /* Micro helper library: shell escaping functions */
10 #define mhl_shell_escape_toesc(x) \
11 (((x)==' ')||((x)=='!')||((x)=='#')||((x)=='$')||((x)=='%')|| \
12 ((x)=='(')||((x)==')')||((x)=='\'')||((x)=='&')||((x)=='~')|| \
13 ((x)=='{')||((x)=='}')||((x)=='[')||((x)==']')||((x)=='`')|| \
14 ((x)=='?')||((x)=='|')||((x)=='<')||((x)=='>')||((x)==';')|| \
15 ((x)=='*')||((x)=='\\')||((x)=='"'))
17 #define mhl_shell_escape_nottoesc(x) \
18 (((x)!=0) && (!mhl_shell_escape_toesc((x))))
20 /** To be compatible with the general posix command lines we have to escape
21 strings for the command line
23 /params const char * in
26 return escaped string (later need to free)
28 static inline char* mhl_shell_escape_dup(const char* src
)
30 if ((src
==NULL
)||(!(*src
)))
33 char* buffer
= calloc(1, strlen(src
)*2+2);
36 /* look for the first char to escape */
40 /* copy over all chars not to escape */
41 while ((c
=(*src
)) && mhl_shell_escape_nottoesc(c
))
48 /* at this point we either have an \0 or an char to escape */
60 /** Unescape paths or other strings for e.g the internal cd
61 shell-unescape within a given buffer (writing to it!)
63 /params const char * src
66 return unescaped string
68 static inline char* mhl_shell_unescape_buf(char* text
)
73 /* look for the first \ - that's quick skipover if there's nothing to escape */
75 while ((*readptr
) && ((*readptr
)!='\\')) readptr
++;
76 if (!(*readptr
)) return text
;
78 /* if we're here, we're standing on the first '\' */
79 char* writeptr
= readptr
;
81 while ((c
= *readptr
))
86 switch ((c
= *readptr
))
88 case 'n': (*writeptr
) = '\n'; writeptr
++; break;
89 case 'r': (*writeptr
) = '\r'; writeptr
++; break;
90 case 't': (*writeptr
) = '\t'; writeptr
++; break;
112 case '\0': /* end of string! malformed escape string */
115 (*writeptr
) = c
; writeptr
++; break;
118 else /* got a normal character */
120 (*writeptr
) = *readptr
;
131 /** Check if char in pointer contain escape'd chars
133 /params const char * in
136 return TRUE if string contain escaped chars
137 otherwise return FALSE
140 mhl_shell_is_char_escaped ( const char *in
)
142 if (in
== NULL
|| !*in
|| in
[0] != '\\')
144 if (mhl_shell_escape_toesc(in
[1]))