Forgot to remove some more .s strings and do a rename in order to prevent compiler...
[midnight-commander.git] / mhl / escape.h
blob3f07d62c6d9dde94416b1e409e4cf522e0a3e5d8
1 #ifndef MHL_ESCAPE_H
2 #define MHL_ESCAPE_H
4 /* Micro helper library: shell escaping functions */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
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
24 string for escaping
25 /returns
26 return escaped string (later need to free)
28 static inline char* mhl_shell_escape_dup(const char* src)
30 if ((src==NULL)||(!(*src)))
31 return strdup("");
33 char* buffer = calloc(1, strlen(src)*2+2);
34 char* ptr = buffer;
36 /* look for the first char to escape */
37 while (1)
39 char c;
40 /* copy over all chars not to escape */
41 while ((c=(*src)) && mhl_shell_escape_nottoesc(c))
43 *ptr = c;
44 ptr++;
45 src++;
48 /* at this point we either have an \0 or an char to escape */
49 if (!c)
50 return buffer;
52 *ptr = '\\';
53 ptr++;
54 *ptr = c;
55 ptr++;
56 src++;
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
64 string for unescaping
65 /returns
66 return unescaped string
68 static inline char* mhl_shell_unescape_buf(char* text)
70 if (!text)
71 return NULL;
73 /* look for the first \ - that's quick skipover if there's nothing to escape */
74 char* readptr = text;
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;
80 char c;
81 while ((c = *readptr))
83 if (c=='\\')
85 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;
92 case ' ':
93 case '\\':
94 case '#':
95 case '$':
96 case '%':
97 case '(':
98 case ')':
99 case '[':
100 case ']':
101 case '{':
102 case '}':
103 case '<':
104 case '>':
105 case '!':
106 case '*':
107 case '?':
108 case '~':
109 case '`':
110 case '"':
111 case ';':
112 case '\0': /* end of string! malformed escape string */
113 goto out;
114 default:
115 (*writeptr) = c; writeptr++; break;
118 else /* got a normal character */
120 (*writeptr) = *readptr;
121 writeptr++;
123 readptr++;
125 out:
126 *writeptr = 0;
128 return text;
131 /** Check if char in pointer contain escape'd chars
133 /params const char * in
134 string for checking
135 /returns
136 return TRUE if string contain escaped chars
137 otherwise return FALSE
139 static inline bool
140 mhl_shell_is_char_escaped ( const char *in )
142 if (in == NULL || !*in || in[0] != '\\')
143 return false;
144 if (mhl_shell_escape_toesc(in[1]))
145 return true;
146 return false;
149 #endif