/* ** specialize() - This routine designed to modify single-letter syllable ** like this: ** a ----> # or $ or % etc. ** u ----> # or $ or % etc. ** etc. ** INPUT: ** char * - single-letter syllable. ** OUTPUT: ** none. ** NOTES: ** none. */ void specialize (char *syllable) { char *tmp; if ( (tmp = (char *)calloc(1, 4)) == NULL) err_sys_fatal("calloc"); if ( strlen (syllable) == 1 ) { (void) gen_rand_symbol(tmp, S_SS); (void)memcpy ((void *)syllable, (void *)tmp, 1); } free ((void *)tmp); }
static int read_from_any_channel(inport **inp) { int i, nelem, error; fd_set read_template; retry: FD_ZERO(&read_template); for(i = 0; i < ninports; i++){ if(inportset[i].in >= 0 && !inportset[i].suspended) FD_SET(inportset[i].in,&read_template); } /* TBmsg("read_from_any_channel, before select\n"); */ if((error = select(FD_SETSIZE, &read_template, NULL, NULL, NULL)) >= 0){ for(i = 0; i < ninports; i++){ if(inportset[i].in >= 0 && FD_ISSET(inportset[i].in,&read_template)) { /* TBmsg("read_from_any_channel, data on port %d\n", inportset[i].in); */ if(inportset[i].in == 0){ nelem = read_from_stdin(); } else if(inportset[i].term_port == TBfalse){ *inp = &inportset[i]; return 0; } else { nelem = multi_read(inportset[i].in); } if(nelem == 0){ err_warn("lost connection with ToolBus"); exit(-1); } if(nelem < 0){ err_sys_fatal("read failed"); /**************/ goto retry; } *inp = &inportset[i]; return nelem; } } } else { if(errno != EINTR) err_sys_warn("select failed"); goto retry; } return TB_ERROR; }
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; }