Fix log entry parser for values containing ":" (colons).

This commit is contained in:
Jonathan Moore Liles 2008-12-02 00:07:45 -06:00
parent a07d997397
commit b22a286fa8
1 changed files with 65 additions and 45 deletions

View File

@ -103,62 +103,82 @@ Log_Entry::parse_alist( const char *s )
int tl = strlen( s ); int tl = strlen( s );
char **r = (char**)malloc( sizeof( char* ) * tl ); char **r = (char**)malloc( sizeof( char* ) * tl );
// const char *e = s + tl; bool quote = false;
bool value = false;
const char *c = NULL; const char *c = NULL;
int i = 0; int i = 0;
for ( ; ; s++ ) for ( ; ; s++ )
{ {
switch ( *s )
/* if ( *s == '\n' ) */
/* break; */
// if ( *s == ':' || s == e )
if ( *s == ':' || *s == '\0' )
{ {
if ( c ) case '\0':
{ case ' ':
int l = s - c; if ( ! quote && c )
char *pair = (char*)malloc( l + 1 );
/* remove trailing space */
if ( c[ l - 1 ] == ' ' )
--l;
strncpy( pair, c, l );
pair[ l ] = '\0';
r[ i++ ] = pair;
/* split */
strtok( pair, " " );
/* remove quotes */
char *v = pair + strlen( pair ) + 1;
unescape( v );
if ( *v == '"' )
{ {
// v++; if ( ! value )
if ( v[ strlen( v ) - 1 ] != '"' )
WARNING( "invalid quoting in log entry!" );
else
{ {
v[ strlen( v ) - 1 ] = '\0'; value = true;
memmove( v, v + 1, strlen( v ) + 1 ); break;
} }
int l = s - c;
char *pair = (char*)malloc( l + 1 );
/* remove trailing space */
if ( c[ l - 1 ] == ' ' )
--l;
strncpy( pair, c, l );
pair[ l ] = '\0';
r[ i++ ] = pair;
/* split */
strtok( pair, " " );
/* remove quotes */
char *v = pair + strlen( pair ) + 1;
unescape( v );
if ( *v == '"' )
{
// v++;
if ( v[ strlen( v ) - 1 ] != '"' )
WARNING( "invalid quoting in log entry!" );
else
{
v[ strlen( v ) - 1 ] = '\0';
memmove( v, v + 1, strlen( v ) + 1 );
}
}
printf( "%s\n", v );
c = NULL;
} }
} break;
case ':': /* this is a key */
c = s; if ( ! quote && ! c )
{
if ( *s == '\0' ) c = s;
value = false;
}
break;
case '"':
quote = !quote;
break;
case '\\':
s++;
break; break;
} }
if ( *s == '\0' )
break;
} }
r[ i ] = NULL; r[ i ] = NULL;