Add mvcp_parser_run_file().
[melted] / src / mvcp / mvcp_parser.c
1 /*
2 * mvcp_parser.c -- MVCP Parser for Melted
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* System header files */
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* Application header files */
28 #include "mvcp_parser.h"
29 #include "mvcp_util.h"
30
31 /** Connect to the parser.
32 */
33
34 mvcp_response mvcp_parser_connect( mvcp_parser parser )
35 {
36 return parser->connect( parser->real );
37 }
38
39 /** Execute a command via the parser.
40 */
41
42 mvcp_response mvcp_parser_execute( mvcp_parser parser, char *command )
43 {
44 return parser->execute( parser->real, command );
45 }
46
47 /** Push a service via the parser.
48 */
49
50 mvcp_response mvcp_parser_received( mvcp_parser parser, char *command, char *doc )
51 {
52 return parser->received != NULL ? parser->received( parser->real, command, doc ) : NULL;
53 }
54
55 /** Push a service via the parser.
56 */
57
58 mvcp_response mvcp_parser_push( mvcp_parser parser, char *command, mlt_service service )
59 {
60 return parser->push( parser->real, command, service );
61 }
62
63 /** Execute a formatted command via the parser.
64 */
65
66 mvcp_response mvcp_parser_executef( mvcp_parser parser, const char *format, ... )
67 {
68 char *command = malloc( 10240 );
69 mvcp_response response = NULL;
70 if ( command != NULL )
71 {
72 va_list list;
73 va_start( list, format );
74 if ( vsnprintf( command, 10240, format, list ) != 0 )
75 response = mvcp_parser_execute( parser, command );
76 va_end( list );
77 free( command );
78 }
79 return response;
80 }
81
82 /** Execute the contents of a file descriptor.
83 */
84
85 mvcp_response mvcp_parser_run_file( mvcp_parser parser, FILE *file )
86 {
87 mvcp_response response = mvcp_response_init( );
88 if ( response != NULL )
89 {
90 char command[ 1024 ];
91 mvcp_response_set_error( response, 201, "OK" );
92 while ( mvcp_response_get_error_code( response ) == 201 && fgets( command, 1024, file ) )
93 {
94 mvcp_util_trim( mvcp_util_chomp( command ) );
95 if ( strcmp( command, "" ) && command[ 0 ] != '#' )
96 {
97 mvcp_response temp = NULL;
98 mvcp_response_printf( response, 1024, "%s\n", command );
99 temp = mvcp_parser_execute( parser, command );
100 if ( temp != NULL )
101 {
102 int index = 0;
103 for ( index = 0; index < mvcp_response_count( temp ); index ++ )
104 mvcp_response_printf( response, 10240, "%s\n", mvcp_response_get_line( temp, index ) );
105 mvcp_response_close( temp );
106 }
107 else
108 {
109 mvcp_response_set_error( response, 500, "Batch execution failed" );
110 }
111 }
112 }
113 }
114 return response;
115 }
116
117 /** Execute the contents of a file. Note the special case mvcp_response returned.
118 */
119
120 mvcp_response mvcp_parser_run( mvcp_parser parser, char *filename )
121 {
122 mvcp_response response;
123 FILE *file = fopen( filename, "r" );
124 if ( file != NULL )
125 {
126 response = mvcp_parser_run_file( parser, file );
127 fclose( file );
128 }
129 else
130 {
131 response = mvcp_response_init( );
132 mvcp_response_set_error( response, 404, "File not found." );
133 }
134 return response;
135 }
136
137 /** Get the notifier associated to the parser.
138 */
139
140 mvcp_notifier mvcp_parser_get_notifier( mvcp_parser parser )
141 {
142 if ( parser->notifier == NULL )
143 parser->notifier = mvcp_notifier_init( );
144 return parser->notifier;
145 }
146
147 /** Close the parser.
148 */
149
150 void mvcp_parser_close( mvcp_parser parser )
151 {
152 if ( parser != NULL )
153 {
154 parser->close( parser->real );
155 mvcp_notifier_close( parser->notifier );
156 free( parser );
157 }
158 }