Add a few options and arguments to mvcp-client.
[melted] / src / mvcp-client / remote.c
1 /*
2 * remote.c -- Remote MVCP client
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 /* System header files */
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include <mvcp/mvcp_remote.h>
29
30 /* Application header files */
31 #include "client.h"
32 #include "io.h"
33
34 static void show_usage( char *program_name )
35 {
36 printf(
37 "Usage: %s [options] [mvcp-command* | file | -]\n"
38 "Options:\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",
45 program_name
46 );
47 }
48
49 /** Connect to a remote server.
50 */
51
52 static mvcp_parser create_parser_prompt( )
53 {
54 char server[ 132 ];
55 int port;
56 mvcp_parser parser = NULL;
57
58 printf( "Connecting to a Server\n\n" );
59
60 printf( "Server [localhost]: " );
61
62 if ( io_get_string( server, sizeof( server ), "localhost" ) != NULL )
63 {
64 printf( "Port [5250]: " );
65
66 if ( get_int( &port, 5250 ) != NULL )
67 parser = mvcp_parser_init_remote( server, port );
68 }
69
70 printf( "\n" );
71
72 return parser;
73 }
74
75 static mvcp_parser create_parser_arg( char *arg )
76 {
77 int port = 5250;
78 char *portstr = strchr( arg, ':' );
79
80 if ( portstr )
81 {
82 port = atoi( &portstr[1] );
83 *portstr = 0;
84 }
85 printf( "Connecting to %s:%d\n", arg, port );
86 return mvcp_parser_init_remote( arg, port );
87 }
88
89 void report( mvcp_response response )
90 {
91 int index = 0;
92 if ( response != NULL )
93 for ( index = 0; index < mvcp_response_count( response ); index ++ )
94 printf( "%s\n", mvcp_response_get_line( response, index ) );
95 }
96
97 static int parse_command_line( mvcp_parser parser, int argc, char **argv )
98 {
99 mvcp_response response = mvcp_parser_connect( parser );
100 int i;
101 int interactive = 1;
102
103 if ( response )
104 {
105 mvcp_response_close( response );
106 response = NULL;
107 for ( i = 1; i < argc; i++ )
108 {
109 // If pipe run stdin
110 if ( !strcmp( argv[i], "-" ) )
111 {
112 response = mvcp_parser_run_file( parser, stdin );
113 report( response );
114 mvcp_response_close( response );
115 interactive = 0;
116 break;
117 }
118 // If '-push unit' option
119 else if ( !strcmp( argv[i], "-push" ) && ( i + 1 ) < argc )
120 {
121 char command[10];
122 char line[1024];
123 char *buffer = NULL;
124 size_t size = 0;
125
126 sprintf( command, "PUSH U%s", argv[++i] );
127 while ( fgets( line, 1024, stdin ) )
128 {
129 if ( !strcmp( line, "" ) )
130 break;
131 buffer = realloc( buffer, size + strlen( line ) );
132 strcat( buffer, line);
133 size += strlen( line ) + 1;
134 }
135 response = mvcp_parser_received( parser, command, buffer );
136 if ( buffer )
137 free( buffer );
138 interactive = 0;
139 }
140 // Skip -s
141 else if ( !strcmp( argv[i], "-s" ) && ( i + 1 ) < argc )
142 {
143 i++;
144 }
145 // If next arg is a readable file run it
146 else if ( argv[i][0] != '-' && !access( argv[i], R_OK ) )
147 {
148 response = mvcp_parser_run( parser, argv[i] );
149 interactive = 0;
150 }
151 // Otherwise execute command
152 else if ( argv[i][0] != '-' )
153 {
154 response = mvcp_parser_execute( parser, argv[i] );
155 interactive = 0;
156 }
157 if ( response )
158 {
159 report( response );
160 mvcp_response_close( response );
161 response = NULL;
162 }
163 }
164 }
165 else
166 {
167 printf( "Failed to connect\n" );
168 }
169 return interactive;
170 }
171
172 /** Main function.
173 */
174
175 int main( int argc, char **argv )
176 {
177 mvcp_parser parser = NULL;
178 int i;
179 int interactive = 1;
180
181 for ( i = 1; i < argc; i++ )
182 {
183 if ( !strncmp( argv[i], "-h", 2 ) || !strncmp( argv[i], "--h", 3 ) )
184 {
185 show_usage( argv[0] );
186 return 0;
187 }
188 else if ( !strcmp( argv[i], "-s" ) && ( i + 1 ) < argc )
189 {
190 parser = create_parser_arg( argv[++i] );
191 if ( parser && argc > 3 )
192 interactive = parse_command_line( parser, argc, argv );
193 }
194 }
195 if ( !parser )
196 {
197 // Prompt for the server
198 parser = create_parser_prompt( );
199 }
200 if ( interactive && parser)
201 {
202 client demo = client_init( parser );
203 client_run( demo );
204 client_close( demo );
205 }
206 mvcp_parser_close( parser );
207 return 0;
208 }