Add missing headers for last change.
[melted] / src / mvcp-console / mvcp-console.c
1 /*
2 * mvcp-console.c -- Local MVCP Test Utility
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 <stdlib.h>
24 #include <string.h>
25 #include <sched.h>
26
27 #ifdef __DARWIN__
28 #include <SDL.h>
29 #endif
30
31 /* Application header files */
32 #include <melted/melted_local.h>
33 #include <melted/melted_commands.h>
34 #include <melted/melted_unit.h>
35 #include <mvcp/mvcp_remote.h>
36 #include <mvcp/mvcp_util.h>
37
38 char *prompt( char *command, int length )
39 {
40 printf( "> " );
41 return fgets( command, length, stdin );
42 }
43
44 void report( mvcp_response response )
45 {
46 int index = 0;
47 if ( response != NULL )
48 for ( index = 0; index < mvcp_response_count( response ); index ++ )
49 printf( "%4d: %s\n", index, mvcp_response_get_line( response, index ) );
50 }
51
52 static void cleanup()
53 {
54 int i;
55 for ( i = 0; i < MAX_UNITS; i++ )
56 melted_unit_close( melted_get_unit(i) );
57 }
58
59 int main( int argc, char **argv )
60 {
61 mvcp_parser parser = NULL;
62 mvcp_response response = NULL;
63 char temp[ 1024 ];
64 int index = 1;
65
66 atexit( cleanup );
67 if ( argc > 2 && !strcmp( argv[ 1 ], "-s" ) )
68 {
69 printf( "Melted Client Instance\n" );
70 parser = mvcp_parser_init_remote( argv[ 2 ], 5250 );
71 response = mvcp_parser_connect( parser );
72 index = 3;
73 }
74 else
75 {
76 printf( "Melted Standalone Instance\n" );
77 parser = melted_parser_init_local( );
78 response = mvcp_parser_connect( parser );
79 }
80
81 if ( response != NULL )
82 {
83 /* process files on command lines before going into console mode */
84 for ( ; index < argc; index ++ )
85 {
86 mvcp_response_close( response );
87 response = mvcp_parser_run( parser, argv[ index ] );
88 report( response );
89 }
90
91 while ( response != NULL && prompt( temp, 1024 ) )
92 {
93 mvcp_util_trim( mvcp_util_chomp( temp ) );
94 if ( !strcasecmp( temp, "BYE" ) )
95 {
96 break;
97 }
98 else if ( strcmp( temp, "" ) )
99 {
100 mvcp_response_close( response );
101 response = mvcp_parser_execute( parser, temp );
102 report( response );
103 }
104 }
105 }
106 else
107 {
108 fprintf( stderr, "Unable to connect to a melted instance.\n" );
109 }
110
111 printf( "\n" );
112 mvcp_parser_close( parser );
113
114 return 0;
115 }