1 #ifndef __MHL_SHELL_ESCAPE_H
2 #define __MHL_SHELL_ESCAPE_H
4 /* Micro helper library: shell escaping functions */
11 #define mhl_shell_escape_toesc(x) \
12 (((x)==' ')||((x)=='!')||((x)=='#')||((x)=='$')||((x)=='%')|| \
13 ((x)=='(')||((x)==')')||((x)=='\'')||((x)=='&')||((x)=='~')|| \
14 ((x)=='{')||((x)=='}')||((x)=='[')||((x)==']')||((x)=='`')|| \
15 ((x)=='?')||((x)=='|')||((x)=='<')||((x)=='>')||((x)==';')|| \
16 ((x)=='*')||((x)=='\\')||((x)=='"'))
18 #define mhl_shell_escape_nottoesc(x) \
19 (((x)!=0) && (!mhl_shell_escape_toesc((x))))
21 /* type for escaped string - just for a bit more type safety ;-p */
22 typedef struct { char* s
; } SHELL_ESCAPED_STR
;
24 /** To be compatible with the general posix command lines we have to escape
25 strings for the command line
27 /params const char * in
30 return escaped string (later need to free)
32 static inline SHELL_ESCAPED_STR
mhl_shell_escape_dup(const char* src
)
34 if ((src
==NULL
)||(!(*src
)))
35 return (SHELL_ESCAPED_STR
){ .s
= strdup("") };
37 char* buffer
= calloc(1, strlen(src
)*2+2);
40 /* look for the first char to escape */
44 /* copy over all chars not to escape */
45 while ((c
=(*src
)) && mhl_shell_escape_nottoesc(c
))
52 /* at this point we either have an \0 or an char to escape */
54 return (SHELL_ESCAPED_STR
){ .s
= buffer
};
64 /** Unescape paths or other strings for e.g the internal cd
65 shell-unescape within a given buffer (writing to it!)
67 /params const char * in
70 return unescaped string
72 static inline char* mhl_shell_unescape_buf(char* text
)
77 /* look for the first \ - that's quick skipover if there's nothing to escape */
79 while ((*readptr
) && ((*readptr
)!='\\')) readptr
++;
80 if (!(*readptr
)) return text
;
82 /* if we're here, we're standing on the first '\' */
83 char* writeptr
= readptr
;
85 while ((c
= *readptr
))
90 switch ((c
= *readptr
))
92 case 'n': (*writeptr
) = '\n'; writeptr
++; break;
93 case 'r': (*writeptr
) = '\r'; writeptr
++; break;
94 case 't': (*writeptr
) = '\t'; writeptr
++; break;
116 case '\0': /* end of line! malformed escape string */
119 (*writeptr
) = c
; writeptr
++; break;
122 else /* got a normal character */
124 (*writeptr
) = *readptr
;
135 /** Check if char in pointer contain escape'd chars
137 /params const char * in
140 return TRUE if string contain escaped chars
141 otherwise return FALSE
144 mhl_shell_is_char_escaped ( const char *in
)
146 if (in
== NULL
|| !*in
|| in
[0] != '\\')
148 if (mhl_shell_escape_toesc(in
[1]))