+
+ return res;
+}
+
+/** @brief Splits a string into a dynar of strings, taking quotes into account
+ *
+ * It basically does the same argument separation than the shell, where white
+ * spaces can be escaped and where arguments are never splitted within a
+ * quote group.
+ * Several subsequent spaces are ignored (unless within quotes, of course).
+ *
+ */
+
+xbt_dynar_t xbt_str_split_quoted(const char *s) {
+ xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string);
+ char *str; /* we have to copy the string before, to handle backslashes */
+ char *beg, *end; /* pointers around the parsed chunk */
+ int in_simple_quote=0, in_double_quote=0;
+ int done = 0;
+ int ctn = 0; /* Got something in this block */
+
+ if (s[0] == '\0')
+ return res;
+ beg = str = xbt_strdup(s);
+
+ /* trim leading spaces */
+ xbt_str_ltrim(beg," ");
+ end=beg;
+
+ while (!done) {
+
+
+ switch (*end) {
+ case '\\':
+ ctn = 1;
+ /* Protected char; move it closer */
+ memmove(end,end+1,strlen(end));
+ if (*end=='\0')
+ THROW0(arg_error,0,"String ends with \\");
+ end++; /* Pass the protected char */
+ break;
+
+ case '\'':
+ ctn = 1;
+ if (!in_double_quote) {
+ in_simple_quote = !in_simple_quote;
+ memmove(end,end+1,strlen(end));
+ } else {
+ /* simple quote protected by double ones */
+ end++;
+ }
+ break;
+ case '"':
+ ctn = 1;
+ if (!in_simple_quote) {
+ in_double_quote = !in_double_quote;
+ memmove(end,end+1,strlen(end));
+ } else {
+ /* double quote protected by simple ones */
+ end++;
+ }
+ break;
+
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\0':
+ if (*end == '\0' && (in_simple_quote || in_double_quote)) {
+ THROW2(arg_error,0,
+ "End of string found while searching for %c in %s",
+ (in_simple_quote?'\'':'"'),
+ s);
+ }
+ if (in_simple_quote || in_double_quote) {
+ end++;
+ } else {
+ if (ctn) {
+ /* Found a separator. Push the string if contains something */
+ char *topush=xbt_malloc(end-beg+1);
+ memcpy(topush,beg,end-beg);
+ topush[end - beg] = '\0';
+ xbt_dynar_push(res,&topush);
+ }
+ ctn= 0;
+
+ if (*end == '\0') {
+ done = 1;
+ break;
+ }
+
+ beg=++end;
+ xbt_str_ltrim(beg," ");
+ end=beg;
+ }
+ break;
+
+ default:
+ ctn = 1;
+ end++;
+ }
+ }
+ free(str);