File: tool.c

package info (click to toggle)
xt-toolbuslib 0.5.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 516 kB
  • ctags: 831
  • sloc: ansic: 5,255; sh: 361; makefile: 89
file content (454 lines) | stat: -rw-r--r-- 11,041 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*

    ToolBus -- The ToolBus Application Architecture
    Copyright (C) 1998-2000  Stichting Mathematisch Centrum, Amsterdam, 
                             The  Netherlands.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/
#include "toolbus.h"
#include "terms.h"
#include "tools.h"
#include "utils.h"
#include "tool.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include "sockets.h"

extern int mkports(TBbool, char *, char *, int *, int *, int *);

static int this_tool_id = -1;      /* tool_id assigned to this tool */

static int ninports = 0;           /* # of connections currently in use */
static inport inportset[TB_MAX_INPORT];

static int toToolBus;       /* port to ToolBus  */
static int fromToolBus;	    /* port from ToolBus */

term_list *tool_in_sign = NULL;  /* used by wish-adapter, etc. */
static term_list *tool_out_sign = NULL;

static term *Snd_Void; /* PROTECTED */

int TBaddTermPort(int in, TBcallbackTerm fun)
{
  int i;

  /* TBmsg("TBaddTermPort(%d,%d), ninports=%d\n", in, fun, ninports); */
  /* First, try to reuse a freed inport */
  for(i=0; i<ninports; i++) {
    if(inportset[i].in == -1) {
      /* Reuse this inport */
      inportset[i].in = in;
      inportset[i].term_port = TBtrue;
      inportset[i].suspended = TBfalse;
      inportset[i].callbackTerm = fun;
      return TB_OK;
    }
  } 
  if(ninports == TB_MAX_INPORT)
    err_fatal("TBaddTermPort -- too many inports");
  inportset[ninports].in = in;
  inportset[ninports].term_port = TBtrue;
  inportset[ninports].callbackTerm = fun;
  ninports++;
  return TB_OK;
}

int TBaddCharPort(int in, TBcallbackChar fun)
{
  int i;

  /* First, try to reuse a freed inport */
  for(i=0; i<ninports; i++) {
    if(inportset[i].in == -1) {
      /* Reuse this inport */
      inportset[i].in = in;
      inportset[i].term_port = TBfalse;
      inportset[i].callbackChar = fun;
      return TB_OK;
    }
  }
 
  if(ninports == TB_MAX_INPORT)
    err_fatal("TBaddCharPort -- too many inports");
  inportset[ninports].in = in;
  inportset[ninports].term_port = TBfalse;
  inportset[ninports].callbackChar = fun;
  ninports++;
  return TB_OK;
}

void TBfreePort(int in)
{
  int i;

  /* Try to find the port to be freed */
  for(i=0; i<ninports; i++) {
    if(inportset[i].in == in)
      inportset[i].in = -1;
  }
} 
    
static void help(void)
{
  char * str =
"\n\
Generic tool options are:\n\
-TB_PORT N            use N as well-known socket of ToolBus\n\
-TB_HOST Name         use the ToolBus running on machine Name\n\
-TB_TOOL_NAME Name    the name of this tool is Name\n\
-TB_TOOL_ID N         the allocated tool id for this tool is N\n\
-TB_SINGLE            stand-alone execution, do connect with ToolBus\n\
\n\n";

  fprintf(stderr, str);
}

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;
}

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 TBgetTid()
{
  return this_tool_id;
}

char *TBgetToolName()
{
  return tool_name;
}

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;
}

static int peek_channels(inport **inp)
{
  int i, error;
  fd_set read_template;
  struct timeval time;

  time.tv_sec = 0;
  time.tv_usec = 0;

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);
  }
  if((error = select(FD_SETSIZE, &read_template,
                     NULL, NULL, &time)) >= 0){
    for(i = 0; i < ninports; i++){
      if(inportset[i].in >= 0 && 
	 FD_ISSET(inportset[i].in,&read_template)) {
        *inp = &inportset[i];
        return 1;
      }
    }
    *inp = NULL;
    return 0;
  }
  if(errno != EINTR)
    err_sys_warn("select failed");
  goto retry;
  /* <PO> unreachable code from lcc: return TB_ERROR; */
}

static term *tool_read_term(void)
{
  int nelem;
  term *trm, *rtrm;
  inport *inp;
  TBbool sndvoid = TBfalse;

  while(TBtrue){
    if(stand_alone){
      fprintf(stdout, "%s", single_prompt);
      fflush(stdout);
    }
    inp = NULL;
    nelem = read_from_any_channel(&inp);
    if(nelem < 0){
      err_warn("tool_read_term: cannot find ready input channel");
      continue;
    }
    if(inp && inp->term_port){
      if((trm = parse_buffer())){
	/*TBmsg("tool_read_term: ***%t***\n", trm);*/
	if(streq(get_txt(fun_sym(trm)), "rec-do"))
          sndvoid = TBtrue;
	rtrm = (*inp->callbackTerm)(trm);
	if(sndvoid)
	  return Snd_Void;
	else
	  return rtrm;
      }
    } else {
      if(inp)
	return (*inp->callbackChar)(inp->in);
    } 
  }
  return NULL; /* <PO> missing return from lcc */
}

/*--- TBsend -------------------------------------*/

void TBsend(term *e)
{
  TBwrite(toToolBus, e);
}

/*--- TBreceive ----------------------------------*/

void TBreceive(void)
{ term *e;

      if((e = tool_read_term()))
	TBsend(e);
      TBcollect();
}

/*--- TBpeek -----------------------------------*/

int TBpeek(void)
{
  inport *port;

  return peek_channels(&port);
}

/*--- TBsuspend --------------------------------*/

/* Suspend the input from a certain port.
   inport == -1 to suspend the input from the standard ToolBus input port. */
void TBsuspend(int inport)
{
  int i;

  if(inport == -1)
    inport = fromToolBus;

  for(i=0; i<ninports; i++) {
    if(inportset[i].in == inport) {
      /* Suspend this inport */
      inportset[i].suspended = TBtrue;
    }
  }
}

/*--- TBresume --------------------------------*/

/* Resume the input from a certain port.
   inport == -1 to suspend the input from the standard ToolBus input port. */
void TBresume(int inport)
{
  int i;

  if(inport == -1)
    inport = fromToolBus;

  for(i=0; i<ninports; i++) {
    if(inportset[i].in == inport) {
      /* Resume inpot from this inport */
      inportset[i].suspended = TBfalse;
    }
  }
}

/*--- TBeventloop ------------------------------*/

void TBeventloop(void)
{
  while(TBtrue)
    TBreceive();
}

/*--- TBmultiloop ------------------------------*/

void TBmultiloop(void)
{
  while(TBtrue) {
    tool_read_term();
    TBcollect();
  }
}