parse_line() is now smart enough to detect quoted strings and escaped characters.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20471 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2007-03-30 13:44:46 +00:00
parent 3a1532ef98
commit 0bfaee2899
+42 -25
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006, Axel Dörfler, [email protected] * Copyright 2002-2007, Axel Dörfler, [email protected]
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Copyright 2001, Travis Geiselbrecht. All rights reserved. * Copyright 2001, Travis Geiselbrecht. All rights reserved.
@@ -75,7 +75,7 @@ static int32 sMessageRepeatCount = 0;
#define HISTORY_SIZE 16 #define HISTORY_SIZE 16
static char sLineBuffer[HISTORY_SIZE][LINE_BUFFER_SIZE] = { "", }; static char sLineBuffer[HISTORY_SIZE][LINE_BUFFER_SIZE] = { "", };
static char sParseLine[LINE_BUFFER_SIZE] = ""; static char sParseLine[LINE_BUFFER_SIZE];
static int32 sCurrentLine = 0; static int32 sCurrentLine = 0;
static char *args[MAX_ARGS] = { NULL, }; static char *args[MAX_ARGS] = { NULL, };
@@ -267,36 +267,53 @@ kgets(char *buffer, int length)
static int static int
parse_line(char *buf, char **argv, int *argc, int max_args) parse_line(const char *buffer, char **argv, int *_argc, int32 maxArgs)
{ {
int pos = 0; char *string = sParseLine;
int32 index = 0;
strcpy(sParseLine, buf); strcpy(string, buffer);
if (sParseLine[0] != '\0' && !isspace(sParseLine[0])) { for (; index < maxArgs && string[0]; index++) {
argv[0] = sParseLine; char quoted;
*argc = 1; char c;
} else
*argc = 0;
while (sParseLine[pos] != '\0') { // skip white space
if (isspace(sParseLine[pos])) { while ((c = string[0]) != '\0' && isspace(c)) {
sParseLine[pos] = '\0'; string++;
// scan all of the whitespace out of this
while (isspace(sParseLine[++pos]))
;
if (sParseLine[pos] == '\0')
break;
argv[*argc] = &sParseLine[pos];
(*argc)++;
if (*argc >= max_args - 1)
break;
} }
pos++; if (!c)
break;
if (c == '\'' || c == '"') {
argv[index] = ++string;
quoted = c;
} else {
argv[index] = string;
quoted = 0;
} }
return *argc; // find end of string
while (string[0]
&& ((quoted && string[0] != quoted)
|| (!quoted && !isspace(string[0])))) {
if (string[0] == '\\') {
// filter out backslashes
strcpy(string, string + 1);
string++;
}
string++;
}
if (string[0]) {
// terminate string
string[0] = '\0';
string++;
}
}
return *_argc = index;
} }