00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "../typehandler.hh"
00024 #include "externaltypehandler.hh"
00025
00026 #ifdef WIN32 // we don't have that forking stuff...
00027
00028
00029 int ExternalTypeHandler::exec_child_io(char *cmd, char **argv, FILE **readpipe, FILE **writepipe)
00030 {
00031 return (-1);
00032 }
00033
00034 ExternalTypeHandler::ExternalTypeHandler(string name, char **decmd, char **encmd, string file_ext) : TypeHandler(name, file_ext) {
00035 encoder_command = 0;
00036 }
00037
00038 ExternalTypeHandler::ExternalTypeHandler(string name, char *decmd, char *encmd, string file_ext) : TypeHandler(name, file_ext) {
00039 encoder_command = 0;
00040 }
00041
00042 ExternalTypeHandler::~ExternalTypeHandler() {
00043 }
00044
00045 void ExternalTypeHandler::run_external(string& data, char **command) {
00046 throw TypeChain::DecodeError( "ExternalTypeHandler not supported on this platform", "(external)" );
00047 }
00048
00049
00050 #else
00051
00052
00053 #include <stdio.h>
00054 #include <unistd.h>
00055 #include <stdlib.h>
00056 #include <sys/time.h>
00057 #include <sys/wait.h>
00058 #include <errno.h>
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 int ExternalTypeHandler::exec_child_io(char *cmd, char **argv, FILE **readpipe, FILE **writepipe)
00085 {
00086 int childpid, pipe1[2], pipe2[2];
00087
00088
00089
00090 if ((pipe(pipe1) < 0) || (pipe(pipe2) < 0)) {
00091 fprintf(stderr, "error creating pipes: %s\n", strerror(errno));
00092 return -1;
00093 }
00094
00095
00096 if ((childpid = vfork()) < 0) {
00097 fprintf(stderr, "error forking!: %s\n", strerror(errno));
00098 return -1;
00099 } else if (childpid > 0) {
00100
00101
00102
00103
00104
00105
00106 close(pipe1[0]);
00107 close(pipe2[1]);
00108
00109
00110
00111
00112 *readpipe = fdopen(pipe2[0],"r");
00113 *writepipe = fdopen(pipe1[1],"w");
00114
00115
00116
00117
00118 return childpid;
00119
00120 } else {
00121
00122
00123 close(pipe1[1]);
00124 close(pipe2[0]);
00125
00126
00127
00128
00129
00130
00131 dup2(pipe1[0],0);
00132 dup2(pipe2[1],1);
00133 close(pipe1[0]);
00134 close(pipe2[1]);
00135
00136
00137
00138
00139 if (execvp(cmd, argv) < 0) {
00140
00141 fprintf(stderr, "external type handler: error executing command: %s\n", strerror(errno));
00142 close(pipe1[0]);
00143 close(pipe1[1]);
00144 close(pipe2[0]);
00145 close(pipe2[1]);
00146
00147
00148 fprintf(stderr, "exiting.\n");
00149 exit(-1);
00150 }
00151
00152
00153 return(-1);
00154 }
00155
00156 return (-1);
00157 }
00158
00159
00160
00161 ExternalTypeHandler::ExternalTypeHandler(string name, char **decmd, char **encmd, string file_ext) : TypeHandler(name, file_ext) {
00162
00163
00164
00165 int n = 0;
00166 int i;
00167 for(n = 0; decmd[n] != 0; n++);
00168 decoder_command = (char**)malloc(n * sizeof(char*) + 1);
00169 for(i = 0; i < n; i++) {
00170 decoder_command[i] = strdup(decmd[i]);
00171 }
00172
00173 if(encmd != 0) {
00174
00175 for(n = 0; encmd[n] != 0; n++);
00176 encoder_command = (char**)malloc(n * sizeof(char*) + 1);
00177 for(i = 0; i < n; i++) {
00178 encoder_command[i] = strdup(encmd[i]);
00179 }
00180 }
00181 else
00182 encoder_command = 0;
00183 }
00184
00185 ExternalTypeHandler::ExternalTypeHandler(string name, char *decmd, char *encmd, string file_ext) : TypeHandler(name, file_ext) {
00186 decoder_command = (char **)malloc(2 * sizeof(char*));
00187 decoder_command[0] = strdup(decmd);
00188 decoder_command[1] = 0;
00189 if(encmd != 0) {
00190 encoder_command = (char **)malloc(2 * sizeof(char*));
00191 encoder_command[0] = strdup(encmd);
00192 encoder_command[1] = 0;
00193 } else {
00194 encoder_command = 0;
00195 }
00196 needtofreecommands = true;
00197 }
00198
00199 ExternalTypeHandler::~ExternalTypeHandler() {
00200 if(needtofreecommands) {
00201 if(decoder_command != 0) {
00202 while(*decoder_command != 0)
00203 free(*(decoder_command++));
00204 free(decoder_command);
00205 }
00206 if(encoder_command != 0) {
00207 while(*encoder_command != 0)
00208 free(*(encoder_command++));
00209 free(encoder_command);
00210 }
00211 }
00212 }
00213
00214 void ExternalTypeHandler::run_external(string& data, char **command) {
00215
00216 int child_pid;
00217 FILE* out;
00218 FILE* in;
00219 int c;
00220
00221 printf("ExternalTypeHandler: running %s...\n", command[0]);
00222 child_pid = exec_child_io(command[0], command, &in, &out);
00223
00224 if (child_pid < 0) {
00225 fprintf(stderr, "error executing external program '%s': %s\n", command[0], strerror(errno));
00226 throw TypeChain::DecodeError( string("Error executing external program ") + command[0] + ": " + strerror(errno), "(external)" );
00227 }
00228 else {
00229 fputs(data.c_str(), out);
00230 fclose(out);
00231 waitpid(child_pid, NULL, WNOHANG);
00232 data = "";
00233 while( (c = getc(in)) != EOF ) {
00234 data += c;
00235 }
00236 fclose(in);
00237 }
00238 }
00239
00240 #endif
00241
00242
00243 void ExternalTypeHandler::decode(string& data, TypeParams& params) {
00244 run_external(data, decoder_command);
00245 }
00246
00247 void ExternalTypeHandler::encode(string& data, TypeParams& params) {
00248 run_external(data, encoder_command);
00249 }
00250