Guard for NULLs
[mono.git] / eglib / src / 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 in_quote = FALSE;
37         gboolean escaped = FALSE;
38         gchar quote_char = '\0';
39         GString *str;
40
41         str = g_string_new ("");
42         ptr = (gchar *) cmdline;
43         while ((c = *ptr++) != '\0') {
44                 if (escaped) {
45                         escaped = FALSE;
46                         if (!g_ascii_isspace (c))
47                                 g_string_append_c (str, c);
48                 } else if (in_quote) {
49                         if (c == quote_char) {
50                                 in_quote = FALSE;
51                                 quote_char = '\0';
52                                 g_ptr_array_add (array, g_string_free (str, FALSE));
53                                 str = g_string_new ("");
54                         } else {
55                                 g_string_append_c (str, c);
56                         }
57                 } else if (g_ascii_isspace (c)) {
58                         if (str->len > 0) {
59                                 g_ptr_array_add (array, g_string_free (str, FALSE));
60                                 str = g_string_new ("");
61                         }
62                 } else if (c == '\\') {
63                         escaped = TRUE;
64                 } else if (c == '\'' || c == '"') {
65                         in_quote = TRUE;
66                         quote_char = c;
67                 } else {
68                         g_string_append_c (str, c);
69                 }
70         }
71
72         if (escaped) {
73                 if (error)
74                         *error = g_error_new (G_LOG_DOMAIN, 0, "Unfinished escape.");
75                 g_string_free (str, TRUE);
76                 return -1;
77         }
78
79         if (in_quote) {
80                 if (error)
81                         *error = g_error_new (G_LOG_DOMAIN, 0, "Unfinished quote.");
82                 g_string_free (str, TRUE);
83                 return -1;
84         }
85
86         if (str->len > 0) {
87                 g_ptr_array_add (array, g_string_free (str, FALSE));
88         } else {
89                 g_string_free (str, TRUE);
90         }
91         g_ptr_array_add (array, NULL);
92         return 0;
93 }
94
95 gboolean
96 g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error)
97 {
98         GPtrArray *array;
99         gint argc;
100         gchar **argv;
101
102         g_return_val_if_fail (command_line, FALSE);
103         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
104
105         array = g_ptr_array_new();
106         if (split_cmdline (command_line, array, error)) {
107                 g_ptr_array_add (array, NULL);
108                 g_strfreev ((gchar **) array->pdata);
109                 g_ptr_array_free (array, FALSE);
110                 return FALSE;
111         }
112
113         argc = array->len;
114         argv = (gchar **) array->pdata;
115
116         if (argc == 1) {
117                 g_strfreev (argv);
118                 g_ptr_array_free (array, FALSE);
119                 return FALSE;
120         }
121
122         if (argcp) {
123                 *argcp = array->len - 1;
124         }
125
126         if (argvp) {
127                 *argvp = argv;
128         } else {
129                 g_strfreev (argv);
130         }
131
132         g_ptr_array_free (array, FALSE);
133         return TRUE;
134 }
135
136 gchar *
137 g_shell_quote (const gchar *unquoted_string)
138 {
139         GString *result = g_string_new ("'");
140         const gchar *p;
141         
142         for (p = unquoted_string; *p; p++){
143                 if (*p == '\'')
144                         g_string_append (result, "'\\'");
145                 g_string_append_c (result, *p);
146         }
147         g_string_append_c (result, '\'');
148         return g_string_free (result, FALSE);
149 }
150
151 gchar *
152 g_shell_unquote (const gchar *quoted_string, GError **error)
153 {
154         GString *result;
155         const char *p;
156         int do_unquote = 0;
157
158         if (quoted_string == NULL)
159                 return NULL;
160         
161         /* Quickly try to determine if we need to unquote or not */
162         for (p = quoted_string; *p; p++){
163                 if (*p == '\'' || *p == '"' || *p == '\\'){
164                         do_unquote = 1;
165                         break;
166                 }
167         }
168         
169         if (!do_unquote)
170                 return g_strdup (quoted_string);
171
172         /* We do need to unquote */
173         result = g_string_new ("");
174         for (p = quoted_string; *p; p++){
175
176                 if (*p == '\''){
177                         /* Process single quote, not even \ is processed by glib's version */
178                         for (p++; *p; p++){
179                                 if (*p == '\'')
180                                         break;
181                                 g_string_append_c (result, *p);
182                         }
183                         if (!*p){
184                                 g_set_error (error, 0, 0, "Open quote");
185                                 return NULL;
186                         }
187                 } else if (*p == '"'){
188                         /* Process double quote, allows some escaping */
189                         for (p++; *p; p++){
190                                 if (*p == '"')
191                                         break;
192                                 if (*p == '\\'){
193                                         p++;
194                                         if (*p == 0){
195                                                 g_set_error (error, 0, 0, "Open quote");
196                                                 return NULL;
197                                         }
198                                         int append = -1;
199                                         switch (*p){
200                                         case '$':
201                                         case '"':
202                                         case '\\':
203                                         case '`':
204                                                 break;
205                                         default:
206                                                 g_string_append_c (result, '\\');
207                                                 break;
208                                         }
209                                 } 
210                                 g_string_append_c (result, *p);
211                         }
212                         if (!*p){
213                                 g_set_error (error, 0, 0, "Open quote");
214                                 return NULL;
215                         }
216                 } else if (*p == '\\'){
217                         p++;
218                         char c = *p;
219                         if (!(c == '$' || c == '"' || c == '\\' || c == '`' || c == 0))
220                                 g_string_append_c (result, '\\');
221                         if (c == 0)
222                                 break;
223                         else
224                                 g_string_append_c (result, c);
225                 } else
226                         g_string_append_c (result, *p);
227         }
228         return g_string_free (result, FALSE);
229 }
230
231 #if JOINT_TEST
232 /*
233  * This test is designed to be built with the 2 glib/eglib to compare
234  */
235
236 char *args [] = {
237         "\\",
238         "\"Foo'bar\"",
239         "'foo'",
240         "'fo\'b'",
241         "'foo\"bar'",
242         "'foo' dingus bar",
243         "'foo' 'bar' 'baz'",
244         "\"foo\" 'bar' \"baz\"",
245         "\"f\\$\\\'",
246         "\"\\",
247         "\\\\",
248         "'\\\\'",
249         "\"f\\$\"\\\"\\\\", //  /\\\"\\\\"
250         "'f\\$'\\\"\\\\", 
251         "'f\\$\\\\'", 
252         NULL
253 };
254
255
256 int
257 main ()
258 {
259         char **s = args;
260         int i;
261         
262         while (*s){
263                 char *r1 = g_shell_unquote (*s, NULL);
264                 char *r2 = g2_shell_unquote (*s, NULL);
265                 char *ok = r1 == r2 ? "ok" : (r1 != NULL && r2 != NULL && strcmp (r1, r2) == 0) ? "ok" : "fail";
266                 
267                 printf ("%s [%s] -> [%s] - [%s]\n", ok, *s, r1, r2);
268                 s++;
269         }
270         return;
271         char buffer [10];
272         buffer [0] = '\"';
273         buffer [1] = '\\';
274         buffer [3] = '\"';
275         buffer [4] = 0;
276         
277         for (i = 32; i < 255; i++){
278                 buffer [2] = i;
279                 printf ("%d [%s] -> [%s]\n", i, buffer, g_shell_unquote (buffer, NULL));
280         }
281 }
282 #endif