cmdline parse

svn path=/trunk/boinc/; revision=994
This commit is contained in:
David Anderson 2003-02-28 23:42:25 +00:00
parent 01b512371c
commit 06031a196a
1 changed files with 43 additions and 12 deletions

View File

@ -137,26 +137,57 @@ void boinc_sleep(int seconds) {
// Modifies the string arg. // Modifies the string arg.
// Returns argc // Returns argc
// TODO: use strtok here // TODO: use strtok here
#define NOT_IN_TOKEN 0
#define IN_SINGLE_QUOTED_TOKEN 1
#define IN_DOUBLE_QUOTED_TOKEN 2
#define IN_UNQUOTED_TOKEN 3
int parse_command_line(char* p, char** argv) { int parse_command_line(char* p, char** argv) {
char** pp = argv; int state = NOT_IN_TOKEN;
bool space = true;
int argc=0; int argc=0;
while (*p) { while (*p) {
if (isspace(*p)) { switch(state) {
*p = 0; case NOT_IN_TOKEN:
space = true; if (isspace(*p)) {
} else { } else if (*p == '\'') {
if (space) { p++;
*pp++ = p; argv[argc++] = p;
argc++; state = IN_SINGLE_QUOTED_TOKEN;
space = false; break;
} else if (*p == '\"') {
p++;
argv[argc++] = p;
state = IN_DOUBLE_QUOTED_TOKEN;
break;
} else {
argv[argc++] = p;
state = IN_UNQUOTED_TOKEN;
} }
break;
case IN_SINGLE_QUOTED_TOKEN:
if (*p == '\'') {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
case IN_DOUBLE_QUOTED_TOKEN:
if (*p == '\"') {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
case IN_UNQUOTED_TOKEN:
if (isspace(*p)) {
*p = 0;
state = NOT_IN_TOKEN;
}
break;
} }
p++; p++;
} }
*pp++ = 0; argv[argc] = 0;
return argc; return argc;
} }