[eglib] (goutput.c) Revert Android logging host-target change
[mono.git] / mono / eglib / gshell.c
1 /*
2  * Shell utility functions.
3  *
4  * Author:
5  *   Gonzalo Paniagua Javier (gonzalo@novell.com
6  *
7  * (C) 2006 Novell, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #include <stdio.h>
29 #include <glib.h>
30
31 static int
32 split_cmdline (const gchar *cmdline, GPtrArray *array, GError **error)
33 {
34         gchar *ptr;
35         gchar c;
36         gboolean escaped = FALSE, fresh = TRUE;
37         gchar quote_char = '\0';
38         GString *str;
39
40         str = g_string_new ("");
41         ptr = (gchar *) cmdline;
42         while ((c = *ptr++) != '\0') {
43                 if (escaped) {
44                         /*
45                          * \CHAR is only special inside a double quote if CHAR is
46                          * one of: $`"\ and newline
47                          */
48                         if (quote_char == '\"'){
49                                 if (!(c == '$' || c == '`' || c == '"' || c == '\\'))
50                                         g_string_append_c (str, '\\');
51                                 g_string_append_c (str, c);
52                         } else {
53                                 if (!g_ascii_isspace (c))
54                                         g_string_append_c (str, c);
55                         }
56                         escaped = FALSE;
57                 } else if (quote_char) {
58                         if (c == quote_char) {
59                                 quote_char = '\0';
60                                 if (fresh && (g_ascii_isspace (*ptr) || *ptr == '\0')){
61                                         g_ptr_array_add (array, g_string_free (str, FALSE));
62                                         str = g_string_new ("");
63                                 }
64                         } else if (c == '\\'){
65                                 escaped = TRUE;
66                         } else 
67                                 g_string_append_c (str, c);
68                 } else if (g_ascii_isspace (c)) {
69                         if (str->len > 0) {
70                                 g_ptr_array_add (array, g_string_free (str, FALSE));
71                                 str = g_string_new ("");
72                         }
73                 } else if (c == '\\') {
74                         escaped = TRUE;
75                 } else if (c == '\'' || c == '"') {
76                         fresh = str->len == 0;
77                         quote_char = c;
78                 } else {
79                         g_string_append_c (str, c);
80                 }
81         }
82
83         if (escaped) {
84                 if (error)
85                         *error = g_error_new (G_LOG_DOMAIN, 0, "Unfinished escape.");
86                 g_string_free (str, TRUE);
87                 return -1;
88         }
89
90         if (quote_char) {
91                 if (error)
92                         *error = g_error_new (G_LOG_DOMAIN, 0, "Unfinished quote.");
93                 g_string_free (str, TRUE);
94                 return -1;
95         }
96
97         if (str->len > 0) {
98                 g_ptr_array_add (array, g_string_free (str, FALSE));
99         } else {
100                 g_string_free (str, TRUE);
101         }
102         g_ptr_array_add (array, NULL);
103         return 0;
104 }
105
106 gboolean
107 g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error)
108 {
109         GPtrArray *array;
110         gint argc;
111         gchar **argv;
112
113         g_return_val_if_fail (command_line, FALSE);
114         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
115
116         array = g_ptr_array_new();
117         if (split_cmdline (command_line, array, error)) {
118                 g_ptr_array_add (array, NULL);
119                 g_strfreev ((gchar **) array->pdata);
120                 g_ptr_array_free (array, FALSE);
121                 return FALSE;
122         }
123
124         argc = array->len;
125         argv = (gchar **) array->pdata;
126
127         if (argc == 1) {
128                 g_strfreev (argv);
129                 g_ptr_array_free (array, FALSE);
130                 return FALSE;
131         }
132
133         if (argcp) {
134                 *argcp = array->len - 1;
135         }
136
137         if (argvp) {
138                 *argvp = argv;
139         } else {
140                 g_strfreev (argv);
141         }
142
143         g_ptr_array_free (array, FALSE);
144         return TRUE;
145 }
146
147 gchar *
148 g_shell_quote (const gchar *unquoted_string)
149 {
150         GString *result = g_string_new ("'");
151         const gchar *p;
152         
153         for (p = unquoted_string; *p; p++){
154                 if (*p == '\'')
155                         g_string_append (result, "'\\'");
156                 g_string_append_c (result, *p);
157         }
158         g_string_append_c (result, '\'');
159         return g_string_free (result, FALSE);
160 }
161
162 gchar *
163 g_shell_unquote (const gchar *quoted_string, GError **error)
164 {
165         GString *result;
166         const char *p;
167         int do_unquote = 0;
168
169         if (quoted_string == NULL)
170                 return NULL;
171         
172         /* Quickly try to determine if we need to unquote or not */
173         for (p = quoted_string; *p; p++){
174                 if (*p == '\'' || *p == '"' || *p == '\\'){
175                         do_unquote = 1;
176                         break;
177                 }
178         }
179         
180         if (!do_unquote)
181                 return g_strdup (quoted_string);
182
183         /* We do need to unquote */
184         result = g_string_new ("");
185         for (p = quoted_string; *p; p++){
186
187                 if (*p == '\''){
188                         /* Process single quote, not even \ is processed by glib's version */
189                         for (p++; *p; p++){
190                                 if (*p == '\'')
191                                         break;
192                                 g_string_append_c (result, *p);
193                         }
194                         if (!*p){
195                                 g_set_error (error, 0, 0, "Open quote");
196                                 return NULL;
197                         }
198                 } else if (*p == '"'){
199                         /* Process double quote, allows some escaping */
200                         for (p++; *p; p++){
201                                 if (*p == '"')
202                                         break;
203                                 if (*p == '\\'){
204                                         p++;
205                                         if (*p == 0){
206                                                 g_set_error (error, 0, 0, "Open quote");
207                                                 return NULL;
208                                         }
209                                         switch (*p){
210                                         case '$':
211                                         case '"':
212                                         case '\\':
213                                         case '`':
214                                                 break;
215                                         default:
216                                                 g_string_append_c (result, '\\');
217                                                 break;
218                                         }
219                                 } 
220                                 g_string_append_c (result, *p);
221                         }
222                         if (!*p){
223                                 g_set_error (error, 0, 0, "Open quote");
224                                 return NULL;
225                         }
226                 } else if (*p == '\\'){
227                         char c = *(++p);
228                         if (!(c == '$' || c == '"' || c == '\\' || c == '`' || c == '\'' || c == 0 ))
229                                 g_string_append_c (result, '\\');
230                         if (c == 0)
231                                 break;
232                         else
233                                 g_string_append_c (result, c);
234                 } else
235                         g_string_append_c (result, *p);
236         }
237         return g_string_free (result, FALSE);
238 }
239
240 #if JOINT_TEST
241 /*
242  * This test is designed to be built with the 2 glib/eglib to compare
243  */
244
245 char *args [] = {
246         "\\",
247         "\"Foo'bar\"",
248         "'foo'",
249         "'fo\'b'",
250         "'foo\"bar'",
251         "'foo' dingus bar",
252         "'foo' 'bar' 'baz'",
253         "\"foo\" 'bar' \"baz\"",
254         "\"f\\$\\\'",
255         "\"\\",
256         "\\\\",
257         "'\\\\'",
258         "\"f\\$\"\\\"\\\\", //  /\\\"\\\\"
259         "'f\\$'\\\"\\\\", 
260         "'f\\$\\\\'", 
261         NULL
262 };
263
264
265 int
266 main ()
267 {
268         char **s = args;
269         int i;
270         
271         while (*s){
272                 char *r1 = g_shell_unquote (*s, NULL);
273                 char *r2 = g2_shell_unquote (*s, NULL);
274                 char *ok = r1 == r2 ? "ok" : (r1 != NULL && r2 != NULL && strcmp (r1, r2) == 0) ? "ok" : "fail";
275                 
276                 printf ("%s [%s] -> [%s] - [%s]\n", ok, *s, r1, r2);
277                 s++;
278         }
279         return;
280         char buffer [10];
281         buffer [0] = '\"';
282         buffer [1] = '\\';
283         buffer [3] = '\"';
284         buffer [4] = 0;
285         
286         for (i = 32; i < 255; i++){
287                 buffer [2] = i;
288                 printf ("%d [%s] -> [%s]\n", i, buffer, g_shell_unquote (buffer, NULL));
289         }
290 }
291 #endif