int TBconnect(char *tname, char *host, int port, TBcallbackTerm fun, term *(*check_in_sign)(term *), int *tid) { int to_tb, from_tb; int old_port; term *tool_in_sign, *trm; /* Fool mkports into assuming a different WellKnown port */ old_port = WellKnownSocketPort; WellKnownSocketPort = port; if(mkports(TBfalse, tname, host, tid, &from_tb, &to_tb) == TB_ERROR) err_fatal("TBconnect -- can't connect to ToolBus"); /* Restore the old WellKnown port */ WellKnownSocketPort = old_port; TBaddTermPort(from_tb, fun); trm = TBread(from_tb); /* obtain the tool signature from the ToolBus */ if(TBmatch(trm, "rec-do(signature(%t,%t))", &tool_in_sign, &tool_out_sign)){ TBwrite(to_tb, Snd_Void); if(check_in_sign){ trm = (*check_in_sign)(tool_in_sign); if(trm) err_fatal("TBconnect -- NOT IN INPUT SIGNATURE: %t", trm); } } else err_fatal("signature information garbled: %t", trm); return to_tb; }
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; }
int TBinit(char *tname, int argc, char *argv[], TBcallbackTerm fun, term * (check_in_sign)(term *)) { char *s; char host_toolbus[MAXHOSTNAMELEN]; int fromToolBus, i = 1; TBbool local_ports = TBfalse; term *trm; tool_name = (tname) ? tname : "anonymous_tool"; ToolBus = TBfalse; init_terms(); init_utils(); if(gethostname(this_host, MAXHOSTNAMELEN) < 0) err_sys_fatal("TBinit -- can't get host name"); strcpy(host_toolbus, this_host); WellKnownSocketPort = TB_PORT; while(i < argc){ if(streq(argv[i], "-help")){ help(); exit(0); } else if(streq(argv[i], "-verbose")){ TBverbose = TBtrue; } else if(streq(argv[i], "-TB_HOST")){ i++; if(strlen(argv[i]) > MAXHOSTNAMELEN) err_fatal("TBinit -- name of ToolBus host too long"); strcpy(host_toolbus, argv[i]); i++; } else if(streq(argv[i],"-TB_PORT")){ i++; WellKnownSocketPort = atoi(argv[i++]); } else if(streq(argv[i],"-TB_TOOL_ID")){ i++; this_tool_id = atoi(argv[i++]); } else if(streq(argv[i], "-TB_TOOL_NAME")){ tool_name = argv[i+1]; i += 2; } else if(streq(argv[i], "-TB_SINGLE")){ i++; stand_alone = TBtrue; } else if(streq(argv[i],"-TB_LOCAL_PORTS")){ i++; local_ports = TBtrue; } else i++; } if((s = getenv("TB_VERBOSE")) && streq(s ,"true")) TBverbose = TBtrue; if((s = getenv("TB_LOCAL_PORTS")) && streq(s ,"true")) local_ports = TBtrue; if(stand_alone || !fun){ /* execute stand alone */ /* TBaddTermPort(0, fun); */ return TB_OK; } if(mkports(local_ports, tool_name, host_toolbus, &this_tool_id, &fromToolBus, &toToolBus) == TB_ERROR) err_fatal("TBinit -- can't connect to ToolBus"); TBaddTermPort(fromToolBus, fun); Snd_Void = TBmake("snd-void()"); TBprotect(&Snd_Void); trm = TBread(fromToolBus); /* obtain the tool signature from the ToolBus */ if(TBmatch(trm, "rec-do(signature(%t,%t))", &tool_in_sign, &tool_out_sign)){ TBsend(Snd_Void); if(check_in_sign){ trm = (*check_in_sign)(tool_in_sign); if(trm) err_fatal("TBinit -- NOT IN INPUT SIGNATURE: %t", trm); } } else err_fatal("signature information garbled: %t", trm); return TB_OK; }
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; }