Index: Makefile =================================================================== --- Makefile (revision 2635) +++ Makefile (revision 2636) @@ -1,2 +1,6 @@ +CFLAGS = -Wall -Wextra -g + +example: example.o qparse.o + qparse.o: qparse.c qparse.h $(CC) -c qparse.c -o qparse.o Index: example.c =================================================================== --- example.c (nonexistent) +++ example.c (revision 2636) @@ -0,0 +1,26 @@ +#include +#include +#include "qparse.h" + +/* Read lines of text from stdin and split them in fields using qparse. */ + +int main() +{ + char s[1024]; + while(fgets(s, sizeof(s), stdin) != NULL) { + int n, argc; + char *end, **argv; + + /* remove trailing newline (if we don't we just get an extra empty field at the end) */ + for(end = s + strlen(s) - 1; (end >= s) && ((*end == '\r') || (*end == '\n')); end--) + *end = '\0'; + + /* split and print fields */ + printf("Splitting '%s':\n", s); + argc = qparse(s, &argv); + for(n = 0; n < argc; n++) + printf(" [%d] '%s'\n", n, argv[n]); + qparse_free(argc, &argv); + } + return 0; +} Index: qparse.c =================================================================== --- qparse.c (revision 2635) +++ qparse.c (revision 2636) @@ -63,12 +63,12 @@ } -int qparse(char *input, char **argv_ret[]) +int qparse(const char *input, char **argv_ret[]) { int argc; int allocated; qp_state_t state; - char *s; + const char *s; char *buff; int buff_len, buff_used; char **argv; Index: qparse.h =================================================================== --- qparse.h (revision 2635) +++ qparse.h (revision 2636) @@ -1,3 +1,9 @@ -int qparse(char *input, char **argv_ret[]); +/* Split input into a list of strings in argv_ret[] using whitepsace as field + separator. It supports double quoted fields and backslash as escape character. + Returns the number of arguments. argv_ret should be free'd using + qparse_free(). */ +int qparse(const char *input, char **argv_ret[]); + +/* Free an argv_ret array allocated by qparse. */ void qparse_free(int argc, char **argv_ret[]);