int TB_connect(int cid) { int r, dummy; term *trm, *in_sign, *out_sign; assert_valid_cid(cid); WellKnownSocketPort = connections[cid]->port; r = mkports(TBfalse, connections[cid]->tool_name, connections[cid]->host, &connections[cid]->tid, &connections[cid]->socket, &dummy); if(r == TB_ERROR) { if(connections[cid]->verbose) TBmsg("TB_connect: failed to connect to ToolBus port %d\n", connections[cid]->port); return -1; } if(connections[cid]->verbose) TBmsg("TB_connect: connected to ToolBus port %d, tid = %d\n", connections[cid]->port, connections[cid]->tid); trm = TBread(connections[cid]->socket); /* obtain the tool signature from the ToolBus */ if(TBmatch(trm, "rec-do(signature(%t,%t))", &in_sign, &out_sign)){ TBwrite(connections[cid]->socket, TBmake("snd-void()")); if(connections[cid]->sigchecker){ trm = (*connections[cid]->sigchecker)(cid, in_sign); if(trm) err_warn("TBconnect -- NOT IN INPUT SIGNATURE: %t", trm); } } else err_fatal("signature information garbled: %t", trm); return 0; }
term *handle_input_from_toolbus(term *e) { char *txt, *cmd_txt, output[MAXOUTPUT], *outp; int n, len, r, status; FILE *from_cmd; term *arg; outp = output; if(TBmatch(e, "rec-terminate(%t)", &arg)){ unlink(tmp_in); unlink(tmp_out); exit(0); } else if(TBmatch(e, "rec-eval(cmd(%s,input(%s)))", &cmd_txt, &txt)){ split_args(cmd_txt); the_cmd = cmd; len = strlen(txt); goto exec_cmd; } else if(TBmatch(e, "rec-eval(input(%s))", &txt)){ the_cmd = def_cmd; len = strlen(txt); goto exec_cmd; } else if(TBmatch(e, "rec-eval(input-term(%t))", &arg)) { the_cmd = def_cmd; txt = TBsprintf("%t", arg); len = strlen(txt); goto exec_cmd; } else if(TBmatch(e, "rec-eval(cmd(%s,input-term(%t)))", &cmd_txt, &arg)){ split_args(cmd_txt); the_cmd = cmd; txt = TBsprintf("%t", arg); len = strlen(txt); goto exec_cmd; } else if(TBmatch(e, "rec-eval(input(%b))", &txt, &len)){ the_cmd = def_cmd; exec_cmd: if(addnewline) txt[len++] = '\n'; exec_cmd(txt, len); while ((r = wait(&status)) != cmd_pid && r != -1) fprintf(stderr, "wait = %d\n", r); if((from_cmd = fopen(tmp_out, "rb")) == NULL) err_sys_fatal("Can't open tmp output file"); /* Mon May 3 17:36:12 MET DST 1999 mdejonge * * When output type != tb_term, we read the output of the command that * we executed (stored in the file tmp_out) as string. When output type * equals tb_term, we use TBreadTerm to read (and parse) the contents of * that file to obtain an term. * * NOTE: With the new aterm library we would probably have used the function * ATparse to obtain an aterm from a string. * */ if( output_type != tb_term ) { while((n=fread(outp, 1, 512, from_cmd)) > 0){ if(outp + n > &output[MAXOUTPUT]) err_fatal("Executed command produces too long output"); outp += n; } if(*(outp-1) == '\n' && !keepnewline) { if(output_type == tb_bstring) outp--; else *(outp-1) = '\0'; } else { if(output_type != tb_bstring) *outp++ = '\0'; } fclose(from_cmd); switch( output_type ) { case tb_bstring: return TBmake(TBfalse, "snd-value(output(%b))", output, outp-output); case tb_string: return TBmake(TBfalse, "snd-value(output(%s))", output); case tb_term: return NULL; } } else { /* Mon May 3 17:36:12 MET DST 1999 mdejonge * * Construct a term from the contents of the file tmp_out by using * the function TBreadTerm. */ term *t; t = TBreadTerm( from_cmd ); fclose(from_cmd ); return TBmake(TBfalse, "snd-value(output(%t))", t ); } } TBmsg("Ignore: %t\n", e); return NULL; }