1 #ifndef __MHL_SHELL_ESCAPE_H
2 #define __MHL_SHELL_ESCAPE_H
4 /* Micro helper library: shell escaping functions */
9 #define mhl_shell_escape_toesc(x) \
10 (((x)==' ')||((x)=='!')||((x)=='#')||((x)=='$')||((x)=='%')|| \
11 ((x)=='(')||((x)==')')||((x)=='\'')||((x)=='&')||((x)=='~')|| \
12 ((x)=='{')||((x)=='}')||((x)=='[')||((x)==']')||((x)=='`')|| \
13 ((x)=='?')||((x)=='|')||((x)=='<')||((x)=='>')||((x)==';')|| \
14 ((x)=='*')||((x)=='\\')||((x)=='"'))
16 #define mhl_shell_escape_nottoesc(x) \
17 (((x)!=0) && (!mhl_shell_escape_toesc((x))))
19 static inline char* mhl_shell_escape_dup(const char* src
)
21 if ((src
==NULL
)||(!(*src
)))
24 char* buffer
= calloc(1, strlen(src
)*2+2);
27 /* look for the first char to escape */
31 /* copy over all chars not to escape */
32 while ((c
=(*src
)) && mhl_shell_escape_nottoesc(c
))
39 /* at this point we either have an \0 or an char to escape */
51 /* shell-unescape within a given buffer (writing to it!) */
52 static inline char* mhl_shell_unescape_buf(char* text
)
57 // look for the first \ - that's quick skipover if there's nothing to escape
59 while ((*readptr
) && ((*readptr
)!='\\')) readptr
++;
60 if (!(*readptr
)) return text
;
62 // if we're here, we're standing on the first '\'
63 char* writeptr
= readptr
;
65 while ((c
= *readptr
))
70 switch ((c
= *readptr
))
72 case 'n': (*writeptr
) = '\n'; writeptr
++; break;
73 case 'r': (*writeptr
) = '\r'; writeptr
++; break;
74 case 't': (*writeptr
) = '\t'; writeptr
++; break;
97 (*writeptr
) = c
; writeptr
++; break;
100 else // got a normal character
102 (*writeptr
) = *readptr
;