2 * remote.c -- Remote MVCP client
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /* System header files */
28 #include <mvcp/mvcp_remote.h>
30 /* Application header files */
34 static void show_usage( char *program_name
)
37 "Usage: %s [options] [mvcp-command* | file | -]\n"
39 " -s host[:port] Set server address and port (default 5250)\n"
40 " -push unit Push MLT XML from stdin/pipe\n"
41 " -h Display usage help\n"
42 "No arguments initiates interactive text client.\n"
43 "The -s option is required to process MVCP commands whether in file, pipe, or command line.\n"
44 "MVCP commands on the command line that contain spaces must be quoted.\n",
49 /** Connect to a remote server.
52 static mvcp_parser
create_parser_prompt( )
56 mvcp_parser parser
= NULL
;
58 printf( "Connecting to a Server\n\n" );
60 printf( "Server [localhost]: " );
62 if ( io_get_string( server
, sizeof( server
), "localhost" ) != NULL
)
64 printf( "Port [5250]: " );
66 if ( get_int( &port
, 5250 ) != NULL
)
67 parser
= mvcp_parser_init_remote( server
, port
);
75 static mvcp_parser
create_parser_arg( char *arg
)
78 char *portstr
= strchr( arg
, ':' );
82 port
= atoi( &portstr
[1] );
85 printf( "Connecting to %s:%d\n", arg
, port
);
86 return mvcp_parser_init_remote( arg
, port
);
89 void report( mvcp_response response
)
92 if ( response
!= NULL
)
93 for ( index
= 0; index
< mvcp_response_count( response
); index
++ )
94 printf( "%s\n", mvcp_response_get_line( response
, index
) );
97 static int parse_command_line( mvcp_parser parser
, int argc
, char **argv
)
99 mvcp_response response
= mvcp_parser_connect( parser
);
105 mvcp_response_close( response
);
107 for ( i
= 1; i
< argc
; i
++ )
110 if ( !strcmp( argv
[i
], "-" ) )
112 response
= mvcp_parser_run_file( parser
, stdin
);
114 mvcp_response_close( response
);
118 // If '-push unit' option
119 else if ( !strcmp( argv
[i
], "-push" ) && ( i
+ 1 ) < argc
)
126 sprintf( command
, "PUSH U%s", argv
[++i
] );
127 while ( fgets( line
, 1024, stdin
) )
129 if ( !strcmp( line
, "" ) )
131 buffer
= realloc( buffer
, size
+ strlen( line
) );
132 strcat( buffer
, line
);
133 size
+= strlen( line
) + 1;
135 response
= mvcp_parser_received( parser
, command
, buffer
);
141 else if ( !strcmp( argv
[i
], "-s" ) && ( i
+ 1 ) < argc
)
145 // If next arg is a readable file run it
146 else if ( argv
[i
][0] != '-' && !access( argv
[i
], R_OK
) )
148 response
= mvcp_parser_run( parser
, argv
[i
] );
151 // Otherwise execute command
152 else if ( argv
[i
][0] != '-' )
154 response
= mvcp_parser_execute( parser
, argv
[i
] );
160 mvcp_response_close( response
);
167 printf( "Failed to connect\n" );
175 int main( int argc
, char **argv
)
177 mvcp_parser parser
= NULL
;
181 for ( i
= 1; i
< argc
; i
++ )
183 if ( !strncmp( argv
[i
], "-h", 2 ) || !strncmp( argv
[i
], "--h", 3 ) )
185 show_usage( argv
[0] );
188 else if ( !strcmp( argv
[i
], "-s" ) && ( i
+ 1 ) < argc
)
190 parser
= create_parser_arg( argv
[++i
] );
191 if ( parser
&& argc
> 3 )
192 interactive
= parse_command_line( parser
, argc
, argv
);
197 // Prompt for the server
198 parser
= create_parser_prompt( );
200 if ( interactive
&& parser
)
202 client demo
= client_init( parser
);
204 client_close( demo
);
206 mvcp_parser_close( parser
);