X-Git-Url: http://research.m1stereo.tv/gitweb?a=blobdiff_plain;f=src%2Fmvcp-client%2Fremote.c;h=5c225b484d1417ba4410de649d6431ffc184f53f;hb=b26b445ded92c4af4c163a687fba7e5a9d1100b1;hp=8171179a0a69db96d20f8010bdde3dad9e118a4d;hpb=27f0329aa8f434794f1f18e018fc3221e58b77a4;p=melted diff --git a/src/mvcp-client/remote.c b/src/mvcp-client/remote.c index 8171179..5c225b4 100644 --- a/src/mvcp-client/remote.c +++ b/src/mvcp-client/remote.c @@ -1,6 +1,6 @@ /* - * remote.c -- Remote Valerie client demo - * Copyright (C) 2002-2003 Ushodaya Enterprises Limited + * remote.c -- Remote MVCP client + * Copyright (C) 2002-2009 Ushodaya Enterprises Limited * Author: Charles Yates * * This program is free software; you can redistribute it and/or modify @@ -21,21 +21,39 @@ /* System header files */ #include #include +#include +#include +#include -#include +#include /* Application header files */ #include "client.h" #include "io.h" +static void show_usage( char *program_name ) +{ + printf( +"Usage: %s [options] [mvcp-command* | file | -]\n" +"Options:\n" +" -s host[:port] Set server address and port (default 5250)\n" +" -push unit Push MLT XML from stdin/pipe\n" +" -h Display usage help\n" +"No arguments initiates interactive text client.\n" +"The -s option is required to process MVCP commands whether in file, pipe, or command line.\n" +"MVCP commands on the command line that contain spaces must be quoted.\n", + program_name + ); +} + /** Connect to a remote server. */ -static valerie_parser create_parser( ) +static mvcp_parser create_parser_prompt( ) { char server[ 132 ]; int port; - valerie_parser parser = NULL; + mvcp_parser parser = NULL; printf( "Connecting to a Server\n\n" ); @@ -46,7 +64,7 @@ static valerie_parser create_parser( ) printf( "Port [5250]: " ); if ( get_int( &port, 5250 ) != NULL ) - parser = valerie_parser_init_remote( server, port ); + parser = mvcp_parser_init_remote( server, port ); } printf( "\n" ); @@ -54,20 +72,137 @@ static valerie_parser create_parser( ) return parser; } +static mvcp_parser create_parser_arg( char *arg ) +{ + int port = 5250; + char *portstr = strchr( arg, ':' ); + + if ( portstr ) + { + port = atoi( &portstr[1] ); + *portstr = 0; + } + printf( "Connecting to %s:%d\n", arg, port ); + return mvcp_parser_init_remote( arg, port ); +} + +void report( mvcp_response response ) +{ + int index = 0; + if ( response != NULL ) + for ( index = 0; index < mvcp_response_count( response ); index ++ ) + printf( "%s\n", mvcp_response_get_line( response, index ) ); +} + +static int parse_command_line( mvcp_parser parser, int argc, char **argv ) +{ + mvcp_response response = mvcp_parser_connect( parser ); + int i; + int interactive = 1; + + if ( response ) + { + mvcp_response_close( response ); + response = NULL; + for ( i = 1; i < argc; i++ ) + { + // If pipe run stdin + if ( !strcmp( argv[i], "-" ) ) + { + response = mvcp_parser_run_file( parser, stdin ); + report( response ); + mvcp_response_close( response ); + interactive = 0; + break; + } + // If '-push unit' option + else if ( !strcmp( argv[i], "-push" ) && ( i + 1 ) < argc ) + { + char command[10]; + char line[1024]; + char *buffer = NULL; + size_t size = 0; + + sprintf( command, "PUSH U%s", argv[++i] ); + while ( fgets( line, 1024, stdin ) ) + { + if ( !strcmp( line, "" ) ) + break; + buffer = realloc( buffer, size + strlen( line ) ); + strcat( buffer, line); + size += strlen( line ) + 1; + } + response = mvcp_parser_received( parser, command, buffer ); + if ( buffer ) + free( buffer ); + interactive = 0; + } + // Skip -s + else if ( !strcmp( argv[i], "-s" ) && ( i + 1 ) < argc ) + { + i++; + } + // If next arg is a readable file run it + else if ( argv[i][0] != '-' && !access( argv[i], R_OK ) ) + { + response = mvcp_parser_run( parser, argv[i] ); + interactive = 0; + } + // Otherwise execute command + else if ( argv[i][0] != '-' ) + { + response = mvcp_parser_execute( parser, argv[i] ); + interactive = 0; + } + if ( response ) + { + report( response ); + mvcp_response_close( response ); + response = NULL; + } + } + } + else + { + printf( "Failed to connect\n" ); + } + return interactive; +} + /** Main function. */ int main( int argc, char **argv ) { - valerie_parser parser = create_parser( ); + mvcp_parser parser = NULL; + int i; + int interactive = 1; - if ( parser != NULL ) + for ( i = 1; i < argc; i++ ) { - dv_demo demo = dv_demo_init( parser ); - dv_demo_run( demo ); - dv_demo_close( demo ); - valerie_parser_close( parser ); + if ( !strncmp( argv[i], "-h", 2 ) || !strncmp( argv[i], "--h", 3 ) ) + { + show_usage( argv[0] ); + return 0; + } + else if ( !strcmp( argv[i], "-s" ) && ( i + 1 ) < argc ) + { + parser = create_parser_arg( argv[++i] ); + if ( parser && argc > 3 ) + interactive = parse_command_line( parser, argc, argv ); + } } - + if ( !parser ) + { + // Prompt for the server + parser = create_parser_prompt( ); + } + if ( interactive && parser) + { + client demo = client_init( parser ); + client_run( demo ); + client_close( demo ); + } + mvcp_parser_close( parser ); return 0; }