2b90fedc4342ab432946fa7f8ae2d5484794800a
[melted] / src / valerie / valerie_parser.c
1 /*
2 * valerie_parser.c -- Valerie Parser for Miracle
3 * Copyright (C) 2002-2003 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 "valerie_parser.h"
29 #include "valerie_util.h"
30
31 /** Connect to the parser.
32 */
33
34 valerie_response valerie_parser_connect( valerie_parser parser )
35 {
36 return parser->connect( parser->real );
37 }
38
39 /** Execute a command via the parser.
40 */
41
42 valerie_response valerie_parser_execute( valerie_parser parser, char *command )
43 {
44 return parser->execute( parser->real, command );
45 }
46
47 /** Push a service via the parser.
48 */
49
50 valerie_response valerie_parser_push( valerie_parser parser, char *command, mlt_service service )
51 {
52 return parser->push( parser->real, command, service );
53 }
54
55 /** Execute a formatted command via the parser.
56 */
57
58 valerie_response valerie_parser_executef( valerie_parser parser, char *format, ... )
59 {
60 char *command = malloc( 10240 );
61 valerie_response response = NULL;
62 if ( command != NULL )
63 {
64 va_list list;
65 va_start( list, format );
66 if ( vsnprintf( command, 10240, format, list ) != 0 )
67 response = valerie_parser_execute( parser, command );
68 va_end( list );
69 free( command );
70 }
71 return response;
72 }
73
74 /** Execute the contents of a file. Note the special case valerie_response returned.
75 */
76
77 valerie_response valerie_parser_run( valerie_parser parser, char *filename )
78 {
79 valerie_response response = valerie_response_init( );
80 if ( response != NULL )
81 {
82 FILE *file = fopen( filename, "r" );
83 if ( file != NULL )
84 {
85 char command[ 1024 ];
86 valerie_response_set_error( response, 201, "OK" );
87 while ( valerie_response_get_error_code( response ) == 201 && fgets( command, 1024, file ) )
88 {
89 valerie_util_trim( valerie_util_chomp( command ) );
90 if ( strcmp( command, "" ) && command[ 0 ] != '#' )
91 {
92 valerie_response temp = NULL;
93 valerie_response_printf( response, 1024, "%s\n", command );
94 temp = valerie_parser_execute( parser, command );
95 if ( temp != NULL )
96 {
97 int index = 0;
98 for ( index = 0; index < valerie_response_count( temp ); index ++ )
99 valerie_response_printf( response, 10240, "%s\n", valerie_response_get_line( temp, index ) );
100 valerie_response_close( temp );
101 }
102 else
103 {
104 valerie_response_set_error( response, 500, "Batch execution failed" );
105 }
106 }
107 }
108 fclose( file );
109 }
110 else
111 {
112 valerie_response_set_error( response, 404, "File not found." );
113 }
114 }
115 return response;
116 }
117
118 /** Get the notifier associated to the parser.
119 */
120
121 valerie_notifier valerie_parser_get_notifier( valerie_parser parser )
122 {
123 if ( parser->notifier == NULL )
124 parser->notifier = valerie_notifier_init( );
125 return parser->notifier;
126 }
127
128 /** Close the parser.
129 */
130
131 void valerie_parser_close( valerie_parser parser )
132 {
133 if ( parser != NULL )
134 {
135 parser->close( parser->real );
136 valerie_notifier_close( parser->notifier );
137 free( parser );
138 }
139 }