[xbuild] Exec task - add support for custom error/warning regex.
[mono.git] / eglib / src / gshell.c
index c54a9eef7cd66a9b5f658e7e8e4261b10a0e990a..8f868337f99e6a73b8a67ca0b5d24aa4482653f0 100644 (file)
@@ -25,7 +25,6 @@
  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <glib.h>
 
@@ -34,8 +33,7 @@ split_cmdline (const gchar *cmdline, GPtrArray *array, GError **error)
 {
        gchar *ptr;
        gchar c;
-       gboolean in_quote = FALSE;
-       gboolean escaped = FALSE;
+       gboolean escaped = FALSE, fresh = TRUE;
        gchar quote_char = '\0';
        GString *str;
 
@@ -43,18 +41,30 @@ split_cmdline (const gchar *cmdline, GPtrArray *array, GError **error)
        ptr = (gchar *) cmdline;
        while ((c = *ptr++) != '\0') {
                if (escaped) {
-                       escaped = FALSE;
-                       if (!g_ascii_isspace (c))
+                       /*
+                        * \CHAR is only special inside a double quote if CHAR is
+                        * one of: $`"\ and newline
+                        */
+                       if (quote_char == '\"'){
+                               if (!(c == '$' || c == '`' || c == '"' || c == '\\'))
+                                       g_string_append_c (str, '\\');
                                g_string_append_c (str, c);
-               } else if (in_quote) {
+                       } else {
+                               if (!g_ascii_isspace (c))
+                                       g_string_append_c (str, c);
+                       }
+                       escaped = FALSE;
+               } else if (quote_char) {
                        if (c == quote_char) {
-                               in_quote = FALSE;
                                quote_char = '\0';
-                               g_ptr_array_add (array, g_string_free (str, FALSE));
-                               str = g_string_new ("");
-                       } else {
+                               if (fresh && g_ascii_isspace (*ptr)){
+                                       g_ptr_array_add (array, g_string_free (str, FALSE));
+                                       str = g_string_new ("");
+                               }
+                       } else if (c == '\\'){
+                               escaped = TRUE;
+                       } else 
                                g_string_append_c (str, c);
-                       }
                } else if (g_ascii_isspace (c)) {
                        if (str->len > 0) {
                                g_ptr_array_add (array, g_string_free (str, FALSE));
@@ -63,7 +73,7 @@ split_cmdline (const gchar *cmdline, GPtrArray *array, GError **error)
                } else if (c == '\\') {
                        escaped = TRUE;
                } else if (c == '\'' || c == '"') {
-                       in_quote = TRUE;
+                       fresh = str->len == 0;
                        quote_char = c;
                } else {
                        g_string_append_c (str, c);
@@ -77,7 +87,7 @@ split_cmdline (const gchar *cmdline, GPtrArray *array, GError **error)
                return -1;
        }
 
-       if (in_quote) {
+       if (quote_char) {
                if (error)
                        *error = g_error_new (G_LOG_DOMAIN, 0, "Unfinished quote.");
                g_string_free (str, TRUE);
@@ -134,3 +144,148 @@ g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GErr
        return TRUE;
 }
 
+gchar *
+g_shell_quote (const gchar *unquoted_string)
+{
+       GString *result = g_string_new ("'");
+       const gchar *p;
+       
+       for (p = unquoted_string; *p; p++){
+               if (*p == '\'')
+                       g_string_append (result, "'\\'");
+               g_string_append_c (result, *p);
+       }
+       g_string_append_c (result, '\'');
+       return g_string_free (result, FALSE);
+}
+
+gchar *
+g_shell_unquote (const gchar *quoted_string, GError **error)
+{
+       GString *result;
+       const char *p;
+       int do_unquote = 0;
+
+       if (quoted_string == NULL)
+               return NULL;
+       
+       /* Quickly try to determine if we need to unquote or not */
+       for (p = quoted_string; *p; p++){
+               if (*p == '\'' || *p == '"' || *p == '\\'){
+                       do_unquote = 1;
+                       break;
+               }
+       }
+       
+       if (!do_unquote)
+               return g_strdup (quoted_string);
+
+       /* We do need to unquote */
+       result = g_string_new ("");
+       for (p = quoted_string; *p; p++){
+
+               if (*p == '\''){
+                       /* Process single quote, not even \ is processed by glib's version */
+                       for (p++; *p; p++){
+                               if (*p == '\'')
+                                       break;
+                               g_string_append_c (result, *p);
+                       }
+                       if (!*p){
+                               g_set_error (error, 0, 0, "Open quote");
+                               return NULL;
+                       }
+               } else if (*p == '"'){
+                       /* Process double quote, allows some escaping */
+                       for (p++; *p; p++){
+                               if (*p == '"')
+                                       break;
+                               if (*p == '\\'){
+                                       p++;
+                                       if (*p == 0){
+                                               g_set_error (error, 0, 0, "Open quote");
+                                               return NULL;
+                                       }
+                                       switch (*p){
+                                       case '$':
+                                       case '"':
+                                       case '\\':
+                                       case '`':
+                                               break;
+                                       default:
+                                               g_string_append_c (result, '\\');
+                                               break;
+                                       }
+                               } 
+                               g_string_append_c (result, *p);
+                       }
+                       if (!*p){
+                               g_set_error (error, 0, 0, "Open quote");
+                               return NULL;
+                       }
+               } else if (*p == '\\'){
+                       char c = *(++p);
+                       if (!(c == '$' || c == '"' || c == '\\' || c == '`' || c == 0))
+                               g_string_append_c (result, '\\');
+                       if (c == 0)
+                               break;
+                       else
+                               g_string_append_c (result, c);
+               } else
+                       g_string_append_c (result, *p);
+       }
+       return g_string_free (result, FALSE);
+}
+
+#if JOINT_TEST
+/*
+ * This test is designed to be built with the 2 glib/eglib to compare
+ */
+
+char *args [] = {
+       "\\",
+       "\"Foo'bar\"",
+       "'foo'",
+       "'fo\'b'",
+       "'foo\"bar'",
+       "'foo' dingus bar",
+       "'foo' 'bar' 'baz'",
+       "\"foo\" 'bar' \"baz\"",
+       "\"f\\$\\\'",
+       "\"\\",
+       "\\\\",
+       "'\\\\'",
+       "\"f\\$\"\\\"\\\\", //  /\\\"\\\\"
+       "'f\\$'\\\"\\\\", 
+       "'f\\$\\\\'", 
+       NULL
+};
+
+
+int
+main ()
+{
+       char **s = args;
+       int i;
+       
+       while (*s){
+               char *r1 = g_shell_unquote (*s, NULL);
+               char *r2 = g2_shell_unquote (*s, NULL);
+               char *ok = r1 == r2 ? "ok" : (r1 != NULL && r2 != NULL && strcmp (r1, r2) == 0) ? "ok" : "fail";
+               
+               printf ("%s [%s] -> [%s] - [%s]\n", ok, *s, r1, r2);
+               s++;
+       }
+       return;
+       char buffer [10];
+       buffer [0] = '\"';
+       buffer [1] = '\\';
+       buffer [3] = '\"';
+       buffer [4] = 0;
+       
+       for (i = 32; i < 255; i++){
+               buffer [2] = i;
+               printf ("%d [%s] -> [%s]\n", i, buffer, g_shell_unquote (buffer, NULL));
+       }
+}
+#endif