Commit 15cb035e authored by Clément Stenac's avatar Clément Stenac

strcmp -> strncmp

Coding style fixes (much more left)
parent 8be514bb
......@@ -43,6 +43,9 @@
#include "vlc_vlm.h"
#include "vlc_vod.h"
#define FREE( p ) \
if( p ) { free( p ); (p) = NULL; }
/*****************************************************************************
* Local prototypes.
*****************************************************************************/
......@@ -77,91 +80,93 @@ static int Manage( vlc_object_t* );
vlm_t *__vlm_New ( vlc_object_t *p_this )
{
vlc_value_t lockval;
vlm_t *vlm = NULL;
vlm_t *p_vlm = NULL;
/* to be sure to avoid multiple creation */
var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
vlc_mutex_lock( lockval.p_address );
if( !(vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
{
msg_Info( p_this, "creating vlm" );
if( ( vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
{
vlc_mutex_unlock( lockval.p_address );
return NULL;
}
vlc_mutex_init( p_this->p_vlc, &vlm->lock );
vlm->i_media = 0;
vlm->media = NULL;
vlm->i_vod = 0;
vlm->i_schedule = 0;
vlm->schedule = NULL;
vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
p_vlm->i_media = 0;
p_vlm->media = NULL;
p_vlm->i_vod = 0;
p_vlm->i_schedule = 0;
p_vlm->schedule = NULL;
vlc_object_yield( vlm );
vlc_object_attach( vlm, p_this->p_vlc );
vlc_object_yield( p_vlm );
vlc_object_attach( p_vlm, p_this->p_vlc );
}
vlc_mutex_unlock( lockval.p_address );
if( vlc_thread_create( vlm, "vlm thread",
if( vlc_thread_create( p_vlm, "vlm thread",
Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
{
vlc_mutex_destroy( &vlm->lock );
vlc_object_destroy( vlm );
vlc_mutex_destroy( &p_vlm->lock );
vlc_object_destroy( p_vlm );
return NULL;
}
return vlm;
return p_vlm;
}
/*****************************************************************************
* vlm_Delete:
*****************************************************************************/
void vlm_Delete( vlm_t *vlm )
void vlm_Delete( vlm_t *p_vlm )
{
vlc_value_t lockval;
var_Get( vlm->p_libvlc, "vlm_mutex", &lockval );
var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
vlc_mutex_lock( lockval.p_address );
vlc_object_release( vlm );
vlc_object_release( p_vlm );
if( vlm->i_refcount > 0 )
if( p_vlm->i_refcount > 0 )
{
vlc_mutex_unlock( lockval.p_address );
return;
}
vlm->b_die = VLC_TRUE;
vlc_thread_join( vlm );
p_vlm->b_die = VLC_TRUE;
vlc_thread_join( p_vlm );
vlc_mutex_destroy( &vlm->lock );
vlc_mutex_destroy( &p_vlm->lock );
while( vlm->i_media ) vlm_MediaDelete( vlm, vlm->media[0], NULL );
if( vlm->media ) free( vlm->media );
while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
FREE( p_vlm->media );
while( vlm->i_schedule ) vlm_ScheduleDelete( vlm, vlm->schedule[0], NULL );
if( vlm->schedule ) free( vlm->schedule );
while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
p_vlm->schedule[0], NULL );
FREE( p_vlm->schedule );
vlc_object_detach( vlm );
vlc_object_destroy( vlm );
vlc_object_detach( p_vlm );
vlc_object_destroy( p_vlm );
vlc_mutex_unlock( lockval.p_address );
}
/*****************************************************************************
* vlm_ExecuteCommand:
*****************************************************************************/
int vlm_ExecuteCommand( vlm_t *vlm, char *command, vlm_message_t **message)
int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
vlm_message_t **pp_message)
{
int result;
int i_result;
vlc_mutex_lock( &vlm->lock );
result = ExecuteCommand( vlm, command, message );
vlc_mutex_unlock( &vlm->lock );
vlc_mutex_lock( &p_vlm->lock );
i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
vlc_mutex_unlock( &p_vlm->lock );
return result;
return i_result;
}
/*****************************************************************************
......@@ -240,48 +245,50 @@ static char *FindEndCommand( char *psz )
*****************************************************************************
* Execute a command which ends with '\0' (string)
*****************************************************************************/
static int ExecuteCommand(vlm_t *vlm, char *command, vlm_message_t **p_message)
static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
vlm_message_t **pp_message )
{
int i_command = 0;
char **p_command = NULL;
char *cmd = command;
vlm_message_t *message = NULL;
char **ppsz_command = NULL;
char *psz_cmd = psz_command;
vlm_message_t *p_message = NULL;
int i, j;
/* First, parse the line and cut it */
while( *cmd != '\0' )
while( *psz_cmd != '\0' )
{
if( *cmd == ' ' || *cmd == '\t' )
if( *psz_cmd == ' ' || *psz_cmd == '\t' )
{
cmd++;
psz_cmd++;
}
else
{
char *p_temp;
char *psz_temp;
int i_temp;
/* support for comments */
if( i_command == 0 && *cmd == '#')
if( i_command == 0 && *psz_cmd == '#')
{
message = vlm_MessageNew( "", NULL );
p_message = vlm_MessageNew( "", NULL );
goto success;
}
p_temp = FindEndCommand( cmd );
psz_temp = FindEndCommand( psz_cmd );
if( p_temp == NULL ) goto error;
if( psz_temp == NULL ) goto error;
i_temp = p_temp - cmd;
i_temp = psz_temp - psz_cmd;
p_command = realloc( p_command, (i_command + 1) * sizeof(char*) );
p_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
strncpy( p_command[ i_command ], cmd, i_temp );
p_command[ i_command ][ i_temp ] = '\0';
ppsz_command = realloc( ppsz_command, (i_command + 1) *
sizeof(char*) );
ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
ppsz_command[ i_command ][ i_temp ] = '\0';
i_command++;
cmd = p_temp;
psz_cmd = psz_temp;
}
}
......@@ -291,11 +298,11 @@ static int ExecuteCommand(vlm_t *vlm, char *command, vlm_message_t **p_message)
if( i_command == 0 )
{
message = vlm_MessageNew( "", NULL );
p_message = vlm_MessageNew( "", NULL );
goto success;
}
if( strcmp(p_command[0], "new") == 0 )
if( strncmp(ppsz_command[0], "new", 3 ) == 0 )
{
int i_type;
......@@ -303,52 +310,52 @@ static int ExecuteCommand(vlm_t *vlm, char *command, vlm_message_t **p_message)
if( i_command < 3 ) goto syntax_error;
/* Get type */
if( strcmp(p_command[2], "vod") == 0 )
if( strncmp(ppsz_command[2], "vod", 3 ) == 0 )
{
i_type = VOD_TYPE;
}
else if( strcmp(p_command[2], "broadcast") == 0 )
else if( strncmp(ppsz_command[2], "broadcast", 9 ) == 0 )
{
i_type = BROADCAST_TYPE;
}
else if( strcmp(p_command[2], "schedule") == 0 )
else if( strncmp(ppsz_command[2], "schedule", 8 ) == 0 )
{
i_type = SCHEDULE_TYPE;
}
else
{
message = vlm_MessageNew( "new", "%s: Choose between vod, "
"broadcast or schedule", p_command[1] );
p_message = vlm_MessageNew( "new", "%s: Choose between vod, "
"broadcast or schedule", ppsz_command[1] );
goto error;
}
/* Check for forbidden media names */
if( strcmp(p_command[1], "all") == 0 ||
strcmp(p_command[1], "media") == 0 ||
strcmp(p_command[1], "schedule") == 0 )
if( strncmp(ppsz_command[1], "all", 3 ) == 0 ||
strncmp(ppsz_command[1], "media", 5) == 0 ||
strncmp(ppsz_command[1], "schedule", 8) == 0 )
{
message = vlm_MessageNew( "new", "\"all\", \"media\" and "
p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
"\"schedule\" are reserved names" );
goto error;
}
/* Check the name is not already in use */
if( vlm_ScheduleSearch( vlm, p_command[1] ) ||
vlm_MediaSearch( vlm, p_command[1] ) )
if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
{
message = vlm_MessageNew( "new", "%s: Name already in use",
p_command[1] );
p_message = vlm_MessageNew( "new", "%s: Name already in use",
ppsz_command[1] );
goto error;
}
/* Schedule */
if( i_type == SCHEDULE_TYPE )
{
vlm_schedule_t *schedule;
schedule = vlm_ScheduleNew( vlm, p_command[1] );
if( !schedule )
vlm_schedule_t *p_schedule;
p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
if( !p_schedule )
{
message = vlm_MessageNew( "new", "could not create schedule" );
p_message = vlm_MessageNew( "new", "could not create schedule" );
goto error;
}
}
......@@ -356,125 +363,127 @@ static int ExecuteCommand(vlm_t *vlm, char *command, vlm_message_t **p_message)
/* Media */
else
{
vlm_media_t *media;
media = vlm_MediaNew( vlm, p_command[1], i_type );
if( !media )
vlm_media_t *p_media;
p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
if( !p_media )
{
message = vlm_MessageNew( "new", "could not create media" );
p_message = vlm_MessageNew( "new", "could not create media" );
goto error;
}
}
if( i_command <= 3 )
{
message = vlm_MessageNew( "new", NULL );
p_message = vlm_MessageNew( "new", NULL );
goto success;
}
/* Properties will be dealt with later on */
}
else if( strcmp(p_command[0], "setup") == 0 )
else if( strncmp(ppsz_command[0], "setup", 5) == 0 )
{
if( i_command < 2 ) goto syntax_error;
/* Properties will be dealt with later on */
}
else if( strcmp(p_command[0], "del") == 0 )
else if( strncmp(ppsz_command[0], "del", 3) == 0 )
{
vlm_media_t *media;
vlm_schedule_t *schedule;
vlm_media_t *p_media;
vlm_schedule_t *p_schedule;
if( i_command < 2 ) goto syntax_error;
media = vlm_MediaSearch( vlm, p_command[1] );
schedule = vlm_ScheduleSearch( vlm, p_command[1] );
p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
if( schedule != NULL )
if( p_schedule != NULL )
{
vlm_ScheduleDelete( vlm, schedule, NULL );
vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
}
else if( media != NULL )
else if( p_media != NULL )
{
vlm_MediaDelete( vlm, media, NULL );
vlm_MediaDelete( p_vlm, p_media, NULL );
}
else if( strcmp(p_command[1], "media") == 0 )
else if( strncmp(ppsz_command[1], "media", 5 ) == 0 )
{
while( vlm->i_media ) vlm_MediaDelete( vlm, vlm->media[0], NULL );
while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
NULL );
}
else if( strcmp(p_command[1], "schedule") == 0 )
else if( strncmp(ppsz_command[1], "schedule", 8) == 0 )
{
while( vlm->i_schedule )
vlm_ScheduleDelete( vlm, vlm->schedule[0], NULL );
while( p_vlm->i_schedule )
vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
}
else if( strcmp(p_command[1], "all") == 0 )
else if( strncmp(ppsz_command[1], "all", 3 ) == 0 )
{
while( vlm->i_media ) vlm_MediaDelete( vlm, vlm->media[0], NULL );
while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
NULL );
while( vlm->i_schedule )
vlm_ScheduleDelete( vlm, vlm->schedule[0], NULL );
while( p_vlm->i_schedule )
vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
}
else
{
message = vlm_MessageNew( "del", "%s: media unknown",
p_command[1] );
p_message = vlm_MessageNew( "del", "%s: media unknown",
ppsz_command[1] );
goto error;
}
message = vlm_MessageNew( "del", NULL );
p_message = vlm_MessageNew( "del", NULL );
goto success;
}
else if( strcmp(p_command[0], "show") == 0 )
else if( strncmp(ppsz_command[0], "show", 4) == 0 )
{
vlm_media_t *media;
vlm_schedule_t *schedule;
vlm_media_t *p_media;
vlm_schedule_t *p_schedule;
if( i_command == 1 )
{
message = vlm_Show( vlm, NULL, NULL, NULL );
p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
goto success;
}
else if( i_command > 2 ) goto syntax_error;
media = vlm_MediaSearch( vlm, p_command[1] );
schedule = vlm_ScheduleSearch( vlm, p_command[1] );
p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
if( schedule != NULL )
if( p_schedule != NULL )
{
message = vlm_Show( vlm, NULL, schedule, NULL );
p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
}
else if( media != NULL )
else if( p_media != NULL )
{
message = vlm_Show( vlm, media, NULL, NULL );
p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
}
else
{
message = vlm_Show( vlm, NULL, NULL, p_command[1] );
p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
}
goto success;
}
else if( strcmp(p_command[0], "help") == 0 )
else if( strncmp( ppsz_command[0], "help", 4 ) == 0 )
{
if( i_command != 1 ) goto syntax_error;
message = vlm_Help( vlm, NULL );
p_message = vlm_Help( p_vlm, NULL );
goto success;
}
else if( strcmp(p_command[0], "control") == 0 )
else if( strncmp(ppsz_command[0], "control", 7) == 0 )
{
vlm_media_t *media;
vlm_media_t *p_media;
if( i_command < 3 ) goto syntax_error;
if( !(media = vlm_MediaSearch( vlm, p_command[1] ) ) )
if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
{
message = vlm_MessageNew( "control", "%s: media unknown",
p_command[1] );
p_message = vlm_MessageNew( "control", "%s: media unknown",
ppsz_command[1] );
goto error;
}
else
......@@ -482,248 +491,265 @@ static int ExecuteCommand(vlm_t *vlm, char *command, vlm_message_t **p_message)
char *psz_command, *psz_arg = 0, *psz_instance = 0;
int i_index = 2;
if( strcmp(p_command[2], "play") && strcmp(p_command[2], "stop") &&
strcmp(p_command[2], "pause") && strcmp(p_command[2], "seek") )
if( strncmp(ppsz_command[2], "play", 4) &&
strncmp(ppsz_command[2], "stop", 4) &&
strncmp(ppsz_command[2], "pause", 5) &&
strncmp(ppsz_command[2], "seek", 4) )
{
i_index++;
psz_instance = p_command[2];
psz_instance = ppsz_command[2];
if( i_command < 4 ) goto syntax_error;
}
psz_command = p_command[i_index];
psz_command = ppsz_command[i_index];
if( i_command >= i_index + 2 ) psz_arg = p_command[i_index + 1];
if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
vlm_MediaControl( vlm, media, psz_instance, psz_command, psz_arg );
message = vlm_MessageNew( "control", NULL );
vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
psz_arg );
p_message = vlm_MessageNew( "control", NULL );
goto success;
}
}
else if( strcmp(p_command[0], "save") == 0 )
else if( strncmp(ppsz_command[0], "save", 4) == 0 )
{
FILE *file;
if( i_command != 2 ) goto syntax_error;
file = fopen( p_command[1], "w" );
file = fopen( ppsz_command[1], "w" );
if( file == NULL )
{
message = vlm_MessageNew( "save", "Unable to save file" );
p_message = vlm_MessageNew( "save", "Unable to save to file" );
goto error;
}
else
{
char *save = vlm_Save( vlm );
fwrite( save, strlen( save ), 1, file );
char *psz_save = vlm_Save( p_vlm );
if( psz_save == NULL )
{
fclose( file );
goto error;
}
fwrite( psz_save, strlen( psz_save ), 1, file );
fclose( file );
free( save );
message = vlm_MessageNew( "save", NULL );
free( psz_save );
p_message = vlm_MessageNew( "save", NULL );
goto success;
}
}
else if( strcmp(p_command[0], "load") == 0 )
else if( strncmp(ppsz_command[0], "load", 4) == 0 )
{
FILE *file;
if( i_command != 2 ) goto syntax_error;
file = fopen( p_command[1], "r" );
file = fopen( ppsz_command[1], "r" );
if( file == NULL )
{
message = vlm_MessageNew( "load", "Unable to load file" );
p_message = vlm_MessageNew( "load", "Unable to load from file" );
goto error;
}
else
{
int64_t size;
char *buffer;
int64_t i_size;
char *psz_buffer;
if( fseek( file, 0, SEEK_END) == 0 )
{
size = ftell( file );
i_size = ftell( file );
fseek( file, 0, SEEK_SET);
buffer = malloc( size + 1 );
fread( buffer, 1, size, file);
buffer[ size ] = '\0';
if( vlm_Load( vlm, buffer ) )
psz_buffer = malloc( i_size + 1 );
if( !psz_buffer )
{
free( buffer );
message = vlm_MessageNew( "load", "error while loading "
fclose( file );
goto error;
}
fread( psz_buffer, 1, i_size, file);
psz_buffer[ i_size ] = '\0';
if( vlm_Load( p_vlm, psz_buffer ) )
{
free( psz_buffer );
p_message = vlm_MessageNew( "load", "error while loading "
"file" );
goto error;
}
free( buffer );
free( psz_buffer );
}
else
{
message = vlm_MessageNew( "load", "read file error" );
p_message = vlm_MessageNew( "load", "read file error" );
goto error;
}
fclose( file );
message = vlm_MessageNew( "load", NULL );
p_message = vlm_MessageNew( "load", NULL );
goto success;
}
}
else
{
message = vlm_MessageNew( p_command[0], "Unknown command" );
p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
goto error;
}
/* Common code between "new" and "setup" */
if( strcmp(p_command[0], "new") == 0 ||
strcmp(p_command[0], "setup") == 0 )
if( strncmp(ppsz_command[0], "new", 3) == 0 ||
strncmp(ppsz_command[0], "setup", 5) == 0 )
{
int i_command_start = strcmp(p_command[0], "new") ? 2 : 3;
vlm_media_t *media;
vlm_schedule_t *schedule;
int i_command_start = strncmp(ppsz_command[0], "new",3 ) ? 2 : 3;
vlm_media_t *p_media;
vlm_schedule_t *p_schedule;
if( i_command < i_command_start ) goto syntax_error;
media = vlm_MediaSearch( vlm, p_command[1] );
schedule = vlm_ScheduleSearch( vlm, p_command[1] );
p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
if( !media && !schedule )
if( !p_media && !p_schedule )
{
message = vlm_MessageNew(