Commit c256165f authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Save a few allocations

parent d1930468
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* malloc(), free() */ #include <stdlib.h> /* malloc(), free() */
#include <ctype.h> /* tolower() */ #include <ctype.h> /* tolower() */
#include <assert.h>
#ifdef ENABLE_VLM #ifdef ENABLE_VLM
...@@ -343,48 +344,50 @@ static int Unescape (char *out, const char *in) ...@@ -343,48 +344,50 @@ static int Unescape (char *out, const char *in)
static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
vlm_message_t **pp_message ) vlm_message_t **pp_message )
{ {
int i_command = 0; size_t i_command = 0;
char **ppsz_command = NULL; char buf[strlen (psz_command) + 1], *psz_buf = buf;
const char *psz_cmd = psz_command; char *ppsz_command[sizeof (buf) / 2];
vlm_message_t *p_message = NULL; vlm_message_t *p_message = NULL;
int i, j;
/* First, parse the line and cut it */ /* First, parse the line and cut it */
while( *psz_cmd != '\0' ) while( *psz_command != '\0' )
{ {
if( *psz_cmd == ' ' || *psz_cmd == '\t' ) const char *psz_temp;
if(isblank (*psz_command))
{ {
psz_cmd++; psz_command++;
continue;
} }
else
/* support for comments */
if( i_command == 0 && *psz_command == '#')
{ {
const char *psz_temp; p_message = vlm_MessageNew( "", NULL );
goto success;
}
/* support for comments */ psz_temp = FindCommandEnd( psz_command );
if( i_command == 0 && *psz_cmd == '#')
{
p_message = vlm_MessageNew( "", NULL );
goto success;
}
psz_temp = FindCommandEnd( psz_cmd ); if( psz_temp == NULL )
{
p_message = vlm_MessageNew( "Incomplete command", psz_command );
goto error;
}
if( psz_temp == NULL ) assert (i_command < (sizeof (ppsz_command) / sizeof (ppsz_command[0])));
{
p_message = vlm_MessageNew( "Incomplete command", psz_cmd );
goto error;
}
ppsz_command = realloc( ppsz_command, (i_command + 1) * ppsz_command[i_command] = psz_buf;
sizeof(char*) ); memcpy (psz_buf, psz_command, psz_temp - psz_command);
ppsz_command[i_command] = strndup (psz_cmd, psz_temp - psz_cmd); psz_buf[psz_temp - psz_command] = '\0';
/* unescape ", ' and \ ... and everything else */ Unescape (psz_buf, psz_buf);
Unescape (ppsz_command[i_command], ppsz_command[i_command]);
i_command++; i_command++;
psz_cmd = psz_temp; psz_buf += psz_temp - psz_command + 1;
} psz_command = psz_temp;
assert (buf + sizeof (buf) >= psz_buf);
} }
/* /*
...@@ -585,7 +588,7 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -585,7 +588,7 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
else else
{ {
char *psz_command, *psz_arg = 0, *psz_instance = 0; char *psz_command, *psz_arg = 0, *psz_instance = 0;
int i_index = 2; size_t i_index = 2;
if( strcmp( ppsz_command[2], "play" ) && if( strcmp( ppsz_command[2], "play" ) &&
strcmp( ppsz_command[2], "stop" ) && strcmp( ppsz_command[2], "stop" ) &&
...@@ -676,7 +679,7 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -676,7 +679,7 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
if( !strcmp(ppsz_command[0], "new") || if( !strcmp(ppsz_command[0], "new") ||
!strcmp(ppsz_command[0], "setup") ) !strcmp(ppsz_command[0], "setup") )
{ {
int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3; size_t i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
vlm_media_t *p_media; vlm_media_t *p_media;
vlm_schedule_t *p_schedule; vlm_schedule_t *p_schedule;
...@@ -694,6 +697,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -694,6 +697,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
if( p_schedule != NULL ) if( p_schedule != NULL )
{ {
size_t i;
for( i = i_command_start ; i < i_command ; i++ ) for( i = i_command_start ; i < i_command ; i++ )
{ {
if( !strcmp( ppsz_command[i], "enabled" ) || if( !strcmp( ppsz_command[i], "enabled" ) ||
...@@ -706,6 +711,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -706,6 +711,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
* command line */ * command line */
else if( !strcmp( ppsz_command[i], "append" ) ) else if( !strcmp( ppsz_command[i], "append" ) )
{ {
size_t j;
if( ++i >= i_command ) break; if( ++i >= i_command ) break;
for( j = i + 1; j < i_command; j++ ) for( j = i + 1; j < i_command; j++ )
...@@ -748,6 +755,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -748,6 +755,8 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
else if( p_media != NULL ) else if( p_media != NULL )
{ {
size_t i;
for( i = i_command_start ; i < i_command ; i++ ) for( i = i_command_start ; i < i_command ; i++ )
{ {
if( !strcmp( ppsz_command[i], "enabled" ) || if( !strcmp( ppsz_command[i], "enabled" ) ||
...@@ -814,20 +823,14 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command, ...@@ -814,20 +823,14 @@ static int ExecuteCommand( vlm_t *p_vlm, const char *psz_command,
} }
success: success:
for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
FREENULL( ppsz_command );
*pp_message = p_message; *pp_message = p_message;
return VLC_SUCCESS; return VLC_SUCCESS;
syntax_error: syntax_error:
p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" ); p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
error: error:
for( i = 0 ; i < i_command ; i++ ) FREENULL( ppsz_command[i] );
FREENULL( ppsz_command );
*pp_message = p_message; *pp_message = p_message;
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -1642,7 +1645,7 @@ static vlm_message_t *vlm_MessageNew( const char *psz_name, ...@@ -1642,7 +1645,7 @@ static vlm_message_t *vlm_MessageNew( const char *psz_name,
vlm_message_t *p_message; vlm_message_t *p_message;
va_list args; va_list args;
if( !psz_name ) return 0; if( !psz_name ) return NULL;
p_message = malloc( sizeof(vlm_message_t) ); p_message = malloc( sizeof(vlm_message_t) );
if( !p_message) if( !p_message)
...@@ -1655,11 +1658,11 @@ static vlm_message_t *vlm_MessageNew( const char *psz_name, ...@@ -1655,11 +1658,11 @@ static vlm_message_t *vlm_MessageNew( const char *psz_name,
if( psz_format ) if( psz_format )
{ {
va_start( args, psz_format ); va_start( args, psz_format );
if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 ) if( vasprintf( &p_message->psz_value, psz_format, args ) == -1 )
{ {
va_end( args ); va_end( args );
free( p_message ); free( p_message );
return 0; return NULL;
} }
va_end( args ); va_end( args );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment