#include #include #include #include #include #include #include "itsdb.h" static char *c_escape_string(const char *); static int _write[2] = {-1, -1}; static int _read[2] = {-1, -1}; static int _output = -1; static int _input = -1; static char *_file = NULL; static char *_command = NULL; static int _size = 0; static char *_buffer = NULL; int echo_create_test_run(const char *data, int run_id, const char *comment, int interactive, int protocol, const char *custom) { if(_command && _file) { FILE *io; if((io = fopen(_file, "w")) == NULL) { fprintf(stderr, "echo_create_test_run(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if char *edata = c_escape_string(data); char *ecomment = c_escape_string(comment); char *ecustom = c_escape_string(custom); fprintf(io, "create-test-run\n\"%s\"\n%d\n\"%s\"\n%d\n%d\n\"%s\"", edata, run_id, ecomment, interactive, protocol, ecustom); free(edata); free(ecomment); free(ecustom); fclose(io); int status = system(_command); if(status) { fprintf(stderr, "echo_create_test_run(): system(3) failure [%d]; exit.\n", errno); exit(-1); } // if if((io = fopen(_file, "r")) == NULL) { fprintf(stderr, "echo_create_test_run(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if while(fgets(_buffer, _size, io)) { capi_printf(_buffer); } // while fclose(io); } // if else { capi_printf("(:application . \"%s\") ", "[incr tsdb()] Native Echo Client"); } // else return 0; } // echo_create_test_run() int echo_process_item(int i_id, const char *i_input, int parse_id, int edges, int nanalyses, int nderivations, int interactive, const char *custom) { if(_command && _file) { FILE *io; if((io = fopen(_file, "w")) == NULL) { fprintf(stderr, "echo_process_item(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if char *einput = c_escape_string(i_input); char *ecustom = c_escape_string(custom); fprintf(io, "process-item\n%d\n\"%s\"\n%d\n%d\n%d\n%d\n%d\n\"%s\"", i_id, einput, parse_id, edges, nanalyses, nderivations, interactive, ecustom); free(einput); free(ecustom); fclose(io); int status = system(_command); if(status) { fprintf(stderr, "echo_process_item(): system(3) failure [%d]; exit.\n", errno); exit(-1); } // if if((io = fopen(_file, "r")) == NULL) { fprintf(stderr, "echo_process_item(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if while(fgets(_buffer, _size, io)) { capi_printf(_buffer); } // while fclose(io); } // if else { capi_printf("(:readings . %d)", 0); } // else return 0; } // echo_process_item() int echo_complete_test_run(int run_id, const char *custom) { if(_command && _file) { FILE *io; if((io = fopen(_file, "w")) == NULL) { fprintf(stderr, "echo_complete_test_run(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if char *ecustom = c_escape_string(custom); fprintf(io, "complete-test-run\n%d\n\"%s\"", run_id, ecustom); free(ecustom); fclose(io); int status = system(_command); if(status) { fprintf(stderr, "echo_complete_test_run(): system(3) failure [%d]; exit.\n", errno); exit(-1); } // if if((io = fopen(_file, "r")) == NULL) { fprintf(stderr, "echo_complete_test_run(): fopen(3) failure [%d]; exit.\n", errno); exit(-1); } // if while(fgets(_buffer, _size, io)) { capi_printf(_buffer); } // while fclose(io); } // if return 0; } // echo_complete_test_run() int echo_reconstruct_item(const char *derivation) { return 0; } // echo_reconstruct_item() int main(int argc, char** argv) { _size = 128 * 1024; if((_buffer = (char *)malloc(_size)) == NULL) { fprintf(stderr, "main(): malloc(2) failure [%d]; exit.\n", errno); exit(-1); } // if if(argc >= 2) { // // _fix_me_ // we should maybe confirm that the argument exists and is executable. // (21-jul-14; oe) if((_file = (char *)malloc(MAXPATHLEN + 1)) == NULL) { fprintf(stderr, "main(): malloc(2) failure [%d]; exit.\n", errno); exit(-1); } // if sprintf(_file, "/tmp/.echo.%s.%d", getlogin(), getpid()); _command = (char *)malloc(strlen(argv[1]) + strlen(_file) + 4); sprintf(_command, "%s '%s'", argv[1], _file); } // if if(!capi_register(echo_create_test_run, echo_process_item, echo_reconstruct_item, echo_complete_test_run)) slave(); exit(0); } // main() char *c_escape_string(const char *string) { /*****************************************************************************\ |* file: |* module: c_escape_string() |* version: |* written by: oe, coli saarbruecken |* last update: 10-apr-97 |* updated by: oe, coli saarbruecken |*****************************************************************************| |* \*****************************************************************************/ char *foo; int i, j; for(i = 0, foo = (char *)string; foo != NULL && *foo; foo++) { if(*foo == '\\' || *foo == '"') { i++; } /* if */ } /* for */ if(i) { foo = (char *)malloc(strlen(string) + i + 1); for(i = j = 0; string[i]; i++, j++) { if(string[i] == '\\' || string[i] == '"') { foo[j++] = '\\'; } /* if */ foo[j] = string[i]; } /* for */ foo[j] = (char)0; return(foo); } /* if */ else { return(string != NULL ? strdup(string) : NULL); } /* else */ } /* c_escape_string() */