Index: example.c =================================================================== --- example.c (revision 6784) +++ example.c (revision 6785) @@ -17,7 +17,7 @@ /* split and print fields */ printf("Splitting '%s':\n", s); - argc = qparse(s, &argv); + argc = qparse2(s, &argv, QPARSE_DOUBLE_QUOTE | QPARSE_SINGLE_QUOTE); for(n = 0; n < argc; n++) printf(" [%d] '%s'\n", n, argv[n]); qparse_free(argc, &argv); Index: qparse.c =================================================================== --- qparse.c (revision 6784) +++ qparse.c (revision 6785) @@ -23,7 +23,8 @@ typedef enum qp_state_e { qp_normal, - qp_dquote + qp_dquote, + qp_squote } qp_state_t; #define qpush(chr) \ @@ -43,6 +44,7 @@ case 't': qpush('\t'); break; \ case 'b': qpush('\b'); break; \ case '"': qpush('"'); break; \ + case '\'': qpush('\''); break; \ case ' ': qpush(' '); break; \ case '\0': break; \ default: \ @@ -63,8 +65,7 @@ buff_used = 0; \ } - -int qparse(const char *input, char **argv_ret[]) +int qparse2(const char *input, char **argv_ret[], flags_t flg) { int argc; int allocated; @@ -92,8 +93,17 @@ qbackslash(*s); break; case '"': - state = qp_dquote; + if (flg & QPARSE_DOUBLE_QUOTE) + state = qp_dquote; + else + qpush(*s); break; + case '\'': + if (flg & QPARSE_SINGLE_QUOTE) + state = qp_squote; + else + qpush(*s); + break; case ' ': case '\t': case '\n': @@ -105,6 +115,7 @@ } /* End of qp_normal */ break; + case qp_dquote: switch (*s) { case '\\': @@ -119,6 +130,21 @@ } /* End of qp_dquote */ break; + + case qp_squote: + switch (*s) { + case '\\': + s++; + qbackslash(*s); + break; + case '\'': + state = qp_normal; + break; + default: + qpush(*s); + } + /* End of qp_dquote */ + break; } } @@ -131,6 +157,12 @@ return argc; } +int qparse(const char *input, char **argv_ret[]) +{ + return qparse2(input, argv_ret, QPARSE_DOUBLE_QUOTE); +} + + void qparse_free(int argc, char **argv_ret[]) { int n; Index: qparse.h =================================================================== --- qparse.h (revision 6784) +++ qparse.h (revision 6785) @@ -9,3 +9,12 @@ /* for C89 - that doesn't have strdup()*/ char *qparse_strdup(const char *s); + +/* More advanced API with more control over the format */ +typedef enum { + QPARSE_DOUBLE_QUOTE = 1, + QPARSE_SINGLE_QUOTE = 2 +} flags_t; + +int qparse2(const char *input, char **argv_ret[], flags_t flg); +