+gchar **
+split_quoted_strings(
+ const gchar *string)
+{
+ char *local = g_strdup(string);
+ char *start = local;
+ char *p = local;
+ char **result;
+ GPtrArray *strs = g_ptr_array_new();
+ int iq = 0;
+
+ while (*p) {
+ if (!iq && *p == ' ') {
+ *p = '\0';
+ g_ptr_array_add(strs, unquote_string(start));
+ start = p+1;
+ } else if (*p == '\\') {
+ /* next character is taken literally; if it's a multicharacter
+ * escape (e.g., \171), that doesn't bother us here */
+ p++;
+ if (!*p) break;
+ } else if (*p == '\"') {
+ iq = ! iq;
+ }
+
+ p++;
+ }
+ if (start != string)
+ g_ptr_array_add(strs, unquote_string(start));
+
+ /* now convert strs into a strv, by stealing its references to the underlying
+ * strings */
+ result = g_new0(char *, strs->len + 1);
+ memmove(result, strs->pdata, sizeof(char *) * strs->len);
+
+ g_ptr_array_free(strs, FALSE); /* FALSE => don't free strings */
+ g_free(local);
+
+ return result;
+}
+
+char *
+strquotedstr(char **saveptr)
+{
+ char * tok = strtok_r(NULL, " ", saveptr);
+ size_t len;
+ int in_quote;
+ int in_backslash;
+ char *p, *t;
+
+ if (!tok)
+ return tok;
+ len = strlen(tok);
+ in_quote = 0;
+ in_backslash = 0;
+ p = tok;
+ while (in_quote || in_backslash || *p != '\0') {
+ if (*p == '\0') {
+ /* append a new token */
+ t = strtok_r(NULL, " ", saveptr);
+ if (!t)
+ return NULL;
+ tok[len] = ' ';
+ len = strlen(tok);
+ }
+ if (!in_backslash) {
+ if (*p == '"')
+ in_quote = !in_quote;
+ else if (*p == '\\') {
+ in_backslash = 1;
+ }
+ } else {
+ in_backslash = 0;
+ }
+ p++;
+ }
+ return tok;
+}
+