[BACK]Return to tcpf.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.77

1.10      noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.11      noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.10      noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.77    ! ohara      47:  * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.76 2018/03/29 01:32:54 noro Exp $
1.10      noro       48: */
1.1       noro       49: #include "ca.h"
                     50: #include "parse.h"
                     51: #include "com.h"
                     52: #include <signal.h>
1.27      noro       53: #include <string.h>
1.70      fujimoto   54: #if !defined(VISUAL) && !defined(__MINGW32__)
1.28      noro       55: #include <unistd.h>
1.1       noro       56: #include <sys/types.h>
                     57: #include <pwd.h>
                     58: #endif
                     59: #include "ox.h"
                     60:
1.63      ohara      61: #include <stdlib.h>
1.70      fujimoto   62: #if defined(VISUAL) || defined(__MINGW32__)
1.19      noro       63: #include <winsock2.h>
1.15      noro       64: #include <process.h>
1.1       noro       65: #endif
                     66:
1.59      ohara      67: #define INIT_TAB_SIZ 64
1.1       noro       68: #define OX_XTERM "ox_xterm"
                     69:
                     70: #if !defined(_PA_RISC1_1)
                     71: #define RSH "rsh"
                     72: #else
                     73: #define RSH "remsh"
                     74: #endif
                     75:
                     76: static struct m_c {
1.76      noro       77:   int m,c,af_unix;
1.1       noro       78: } *m_c_tab;
                     79:
                     80: static int m_c_i,m_c_s;
1.77    ! ohara      81: extern int I_am_server;
1.1       noro       82:
1.43      noro       83: extern int little_endian;
                     84:
1.39      noro       85: #if defined(MPI)
1.1       noro       86: extern int mpi_nprocs;
                     87: #define valid_mctab_index(ind)\
1.8       noro       88: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
1.16      noro       89: #define check_valid_mctab_index(ind)\
                     90: if((ind)<0||(ind)>=mpi_nprocs){(ind)=-1;}
1.1       noro       91: #else
                     92: #define valid_mctab_index(ind)\
1.54      noro       93: if((ind)<I_am_server||(ind)>=m_c_i||\
1.8       noro       94: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
1.16      noro       95: #define check_valid_mctab_index(ind)\
1.54      noro       96: if((ind)<I_am_server||(ind)>=m_c_i||\
1.16      noro       97: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){(ind)=-1;}
1.1       noro       98: #endif
                     99:
1.44      noro      100: struct IOFP iofp_102[MAXIOFP];
                    101: int nserver_102;
                    102: int myrank_102;
1.43      noro      103:
                    104: int register_102(int s,int rank, int is_master);
                    105:
1.1       noro      106: int register_server();
                    107: int get_mcindex(int);
                    108:
1.8       noro      109: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
                    110:
1.26      noro      111: void Pox_launch(),Pox_launch_nox();
1.1       noro      112: void Pox_launch_generic();
                    113: void Pox_shutdown();
                    114:
1.5       noro      115: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
1.1       noro      116:
                    117: void Pox_push_local(),Pox_push_cmo(),Pox_push_vl(),Pox_push_cmd();
                    118:
                    119: void Pox_pop_local(),Pox_pop_cmo();
                    120: void Pox_pop0_local(),Pox_pop0_cmo();
                    121: void Pox_pop_string(),Pox_pop0_string();
                    122: void Pox_get(),Pox_pops();
                    123:
                    124: void Pox_execute_function(),Pox_execute_string();
                    125: void Pox_setname(), Pox_evalname();
                    126: void Pox_flush();
                    127: void Pgenerate_port(),Ptry_bind_listen(),Ptry_connect(),Ptry_accept();
                    128: void Pregister_server();
                    129: void Pox_get_serverinfo();
1.8       noro      130: void Pox_mpi_myid(), Pox_mpi_nprocs();
1.44      noro      131: void Pox_tcp_accept_102(),Pox_tcp_connect_102();
                    132: void Pox_send_102(),Pox_recv_102();
                    133: void Pox_set_rank_102();
1.49      noro      134: void Pox_get_rank_102();
1.44      noro      135: void Pox_reset_102();
1.46      noro      136: void Pox_bcast_102();
1.47      noro      137: void Pox_reduce_102();
1.1       noro      138:
                    139: void ox_launch_generic();
                    140:
                    141: pointer bevalf();
                    142:
                    143: struct ftab tcp_tab[] = {
1.76      noro      144:   {"ox_send_raw_cmo",Pox_send_raw_cmo,2},
                    145:   {"ox_recv_raw_cmo",Pox_recv_raw_cmo,1},
                    146:   {"ox_get_serverinfo",Pox_get_serverinfo,-1},
                    147:   {"generate_port",Pgenerate_port,-1},
                    148:
                    149:   /* from master to client */
                    150:   {"ox_set_rank_102",Pox_set_rank_102,3},
                    151:   {"ox_tcp_accept_102",Pox_tcp_accept_102,3},
                    152:   {"ox_tcp_connect_102",Pox_tcp_connect_102,4},
                    153:   {"ox_reset_102",Pox_reset_102,1},
                    154:
                    155:   {"ox_get_rank_102",Pox_get_rank_102,0},
                    156:   {"ox_send_102",Pox_send_102,2},
                    157:   {"ox_recv_102",Pox_recv_102,1},
                    158:   {"ox_bcast_102",Pox_bcast_102,-2},
                    159:   {"ox_reduce_102",Pox_reduce_102,-3},
                    160:
                    161:   {"try_bind_listen",Ptry_bind_listen,1},
                    162:   {"try_connect",Ptry_connect,2},
                    163:   {"try_accept",Ptry_accept,2},
                    164:   {"register_server",Pregister_server,4},
                    165:   {"ox_flush",Pox_flush,1},
                    166:   {"ox_launch",Pox_launch,-3},
                    167:   {"ox_launch_nox",Pox_launch_nox,-3},
                    168:   {"ox_launch_generic",Pox_launch_generic,7},
                    169:   {"ox_shutdown",Pox_shutdown,1},
1.1       noro      170:
1.76      noro      171:   {"ox_rpc",Pox_rpc,-99999999},
                    172:   {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
1.1       noro      173:
1.76      noro      174:   {"ox_sync",Pox_sync,1},
1.39      noro      175: #if defined(MPI)
1.76      noro      176:   {"ox_mpi_myid",Pox_mpi_myid,0},
                    177:   {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
1.8       noro      178: #endif
1.39      noro      179: #if !defined(MPI)
1.76      noro      180:   {"ox_reset",Pox_reset,-2},
                    181:   {"ox_intr",Pox_intr,1},
                    182:   {"ox_select",Pox_select,-2},
                    183: #endif
                    184:
                    185:   {"ox_pop_string",Pox_pop_string,1},
                    186:   {"ox_pop0_string",Pox_pop0_string,1},
                    187:   {"ox_pop_local",Pox_pop_local,1},
                    188:   {"ox_pop0_local",Pox_pop0_local,1},
                    189:   {"ox_pop_cmo",Pox_pop_cmo,1},
                    190:   {"ox_pop0_cmo",Pox_pop0_cmo,1},
                    191:   {"ox_get",Pox_get,-1},
                    192:   {"ox_pops",Pox_pops,-2},
                    193:
                    194:   {"ox_push_vl",Pox_push_vl,1},
                    195:   {"ox_push_local",Pox_push_local,-99999999},
                    196:   {"ox_push_cmo",Pox_push_cmo,-99999999},
                    197:   {"ox_push_cmd",Pox_push_cmd,2},
1.1       noro      198:
1.76      noro      199:   {"ox_setname",Pox_setname,2},
                    200:   {"ox_evalname",Pox_evalname,2},
1.1       noro      201:
1.76      noro      202:   {"ox_execute_string",Pox_execute_string,2},
                    203:   {"ox_execute_function",Pox_execute_function,3},
1.1       noro      204:
1.76      noro      205:   {0,0,0},
1.1       noro      206: };
                    207:
                    208: extern struct IOFP iofp[];
                    209: extern MATHCAP my_mathcap;
1.6       noro      210: extern int ox_exchange_mathcap;
1.1       noro      211:
1.63      ohara     212: Obj asir_pop_one();
                    213: void asir_push_one(Obj);
1.1       noro      214:
1.39      noro      215: #if defined(MPI)
1.8       noro      216: extern int mpi_myid, mpi_nprocs;
                    217:
1.26      noro      218: void Pox_mpi_myid(Q *rp)
1.8       noro      219: {
1.76      noro      220:   STOQ(mpi_myid,*rp);
1.8       noro      221: }
                    222:
1.26      noro      223: void Pox_mpi_nprocs(Q *rp)
1.8       noro      224: {
1.76      noro      225:   STOQ(mpi_nprocs,*rp);
1.8       noro      226: }
                    227: #endif
                    228:
1.26      noro      229: void Pox_get_serverinfo(NODE arg,LIST *rp)
1.1       noro      230: {
1.76      noro      231:   int i,c;
                    232:   Q s_id;
                    233:   NODE t,n0,n;
                    234:   LIST list,l;
                    235:
                    236:   if ( !arg ) {
                    237:     for ( i = I_am_server?1:0, n0 = 0; i < m_c_i; i++ )
                    238:       if ( (m_c_tab[i].m>=0) || (m_c_tab[i].c>=0) ) {
                    239:         c = m_c_tab[i].c;
                    240:         ox_get_serverinfo(c,&list);
                    241:         STOQ(i,s_id);
                    242:         t = mknode(2,s_id,list);
                    243:         MKLIST(l,t);
                    244:         NEXTNODE(n0,n);
                    245:         BDY(n) = (pointer)l;
                    246:       }
                    247:     if ( n0 )
                    248:       NEXT(n) = 0;
                    249:     MKLIST(*rp,n0);
                    250:   } else {
                    251:     i = QTOS((Q)ARG0(arg));
                    252:     if ( i >= 0 && i < m_c_i && ((m_c_tab[i].m>=0) || (m_c_tab[i].c>=0)) )
                    253:       ox_get_serverinfo(m_c_tab[i].c,rp);
                    254:     else {
                    255:       MKLIST(*rp,0);
                    256:     }
                    257:   }
1.1       noro      258: }
                    259:
                    260: /*
                    261:   if noarg or arg[0]==0 then use IP socket and return a port number
                    262:   else use UNIX socket and return a string which represents a path name
                    263: */
                    264:
1.26      noro      265: void Pgenerate_port(NODE arg,Obj *rp)
1.1       noro      266: {
1.76      noro      267:   char port_str[BUFSIZ];
                    268:   int port;
                    269:   char *s;
                    270:   STRING str;
                    271:   Q q;
                    272:
                    273:   if ( !arg || !ARG0(arg) ) {
                    274:     generate_port(0,port_str);
                    275:     port = atoi(port_str);
                    276:     STOQ(port,q);
                    277:     *rp = (Obj)q;
                    278:   } else {
                    279:     generate_port(1,port_str);
                    280:     s = (char *)MALLOC(strlen((char *)port_str)+1);
                    281:     strcpy(s,port_str);
                    282:     MKSTR(str,s);
                    283:     *rp = (Obj)str;
                    284:   }
1.1       noro      285: }
                    286:
1.44      noro      287: void Pox_reset_102(NODE arg,Q *rp)
                    288: {
1.76      noro      289:   int s;
                    290:   int  index = QTOS((Q)ARG0(arg));
1.44      noro      291:
1.76      noro      292:   valid_mctab_index(index);
                    293:   s = m_c_tab[index].c;
                    294:   ox_send_cmd(s,SM_reset_102);
                    295:   ox_flush_stream_force(s);
                    296:   *rp = 0;
1.49      noro      297: }
                    298:
                    299: void Pox_get_rank_102(LIST *rp)
                    300: {
1.76      noro      301:   Q n,r;
                    302:   NODE node;
1.49      noro      303:
1.76      noro      304:   STOQ(nserver_102,n);
                    305:   STOQ(myrank_102,r);
                    306:   node = mknode(2,n,r);
                    307:   MKLIST(*rp,node);
1.44      noro      308: }
                    309:
                    310: void Pox_set_rank_102(NODE arg,Q *rp)
                    311: {
1.76      noro      312:   Q nserver,rank;
                    313:   int s;
                    314:   int  index = QTOS((Q)ARG0(arg));
                    315:
                    316:   valid_mctab_index(index);
                    317:   s = m_c_tab[index].c;
                    318:   nserver = (Q)ARG1(arg);
                    319:   rank = (Q)ARG2(arg);
                    320:   ox_send_data(s,nserver);
                    321:   ox_send_data(s,rank);
                    322:   ox_send_cmd(s,SM_set_rank_102);
                    323:   ox_flush_stream_force(s);
                    324:   *rp = 0;
1.44      noro      325: }
                    326:
1.45      noro      327: /* ox_tcp_accept_102(server,port,rank) */
1.43      noro      328:
1.44      noro      329: void Pox_tcp_accept_102(NODE arg,Q *rp)
1.43      noro      330: {
1.76      noro      331:   int s;
                    332:   int  index = QTOS((Q)ARG0(arg));
1.45      noro      333:
1.76      noro      334:   valid_mctab_index(index);
                    335:   s = m_c_tab[index].c;
1.43      noro      336:
1.76      noro      337:   ox_send_data(s,ARG1(arg));
                    338:   ox_send_data(s,ARG2(arg));
                    339:   ox_send_cmd(s,SM_tcp_accept_102);
                    340:   ox_flush_stream_force(s);
                    341:   *rp = 0;
1.43      noro      342: }
                    343:
                    344: /*
1.45      noro      345:  ox_tcp_connect_102(server,host,port,rank)
1.43      noro      346: */
                    347:
1.44      noro      348: void Pox_tcp_connect_102(NODE arg,Q *rp)
1.43      noro      349: {
1.76      noro      350:   int s;
                    351:   int  index = QTOS((Q)ARG0(arg));
1.45      noro      352:
1.76      noro      353:   valid_mctab_index(index);
                    354:   s = m_c_tab[index].c;
1.43      noro      355:
1.76      noro      356:   ox_send_data(s,ARG1(arg));
                    357:   ox_send_data(s,ARG2(arg));
                    358:   ox_send_data(s,ARG3(arg));
                    359:   ox_send_cmd(s,SM_tcp_connect_102);
                    360:   ox_flush_stream_force(s);
                    361:   *rp = 0;
1.43      noro      362: }
                    363:
1.1       noro      364: /*
                    365:  try_bind_listen(port)
                    366: */
                    367:
1.26      noro      368: void Ptry_bind_listen(NODE arg,Q *rp)
1.1       noro      369: {
1.76      noro      370:   char port_str[BUFSIZ];
                    371:   int port,s,use_unix;
1.1       noro      372:
1.76      noro      373:   if ( IS_CYGWIN || !ARG0(arg) || NUM(ARG0(arg)) ) {
                    374:     port = QTOS((Q)ARG0(arg));
                    375:     sprintf(port_str,"%d",port);
                    376:     use_unix = 0;
                    377:   } else {
                    378:     strcpy(port_str,BDY((STRING)ARG0(arg)));
                    379:     use_unix = 1;
                    380:   }
                    381:   s = try_bind_listen(use_unix,port_str);
                    382:   STOQ(s,*rp);
1.1       noro      383: }
                    384:
                    385: /*
                    386:  try_connect(host,port)
                    387: */
                    388:
1.26      noro      389: void Ptry_connect(NODE arg,Q *rp)
1.1       noro      390: {
1.76      noro      391:   char port_str[BUFSIZ];
                    392:   char *host;
                    393:   int port,s,use_unix;
                    394:
                    395:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
                    396:     port = QTOS((Q)ARG1(arg));
                    397:     sprintf(port_str,"%d",port);
                    398:     use_unix = 0;
                    399:   } else {
                    400:     strcpy(port_str,BDY((STRING)ARG1(arg)));
                    401:     use_unix = 1;
                    402:   }
                    403:   host = BDY((STRING)ARG0(arg));
                    404:   s = try_connect(use_unix,host,port_str);
                    405:   STOQ(s,*rp);
1.1       noro      406: }
                    407:
                    408: /*
                    409:  try_accept(sock,port)
                    410: */
                    411:
1.26      noro      412: void Ptry_accept(NODE arg,Q *rp)
1.1       noro      413: {
1.76      noro      414:   int use_unix,s;
1.1       noro      415:
1.76      noro      416:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) )
                    417:     use_unix = 0;
                    418:   else
                    419:     use_unix = 1;
                    420:   s = try_accept(use_unix,QTOS((Q)ARG0(arg)));
                    421:   STOQ(s,*rp);
1.1       noro      422: }
                    423:
                    424: /*
                    425:  register_server(cs,cport,ss,sport)
                    426: */
                    427:
1.26      noro      428: void Pregister_server(NODE arg,Q *rp)
1.1       noro      429: {
1.76      noro      430:   int cs,ss,cn,sn,ind,use_unix,id;
                    431:   char cport_str[BUFSIZ],sport_str[BUFSIZ];
                    432:   Obj obj;
                    433:   MATHCAP server_mathcap;
                    434:
                    435:   cs = QTOS((Q)ARG0(arg));
                    436:   ss = QTOS((Q)ARG2(arg));
                    437:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
                    438:     sprintf(cport_str,"%d",QTOS((Q)ARG1(arg)));
                    439:     use_unix = 0;
                    440:   } else {
                    441:     strcpy(cport_str,BDY((STRING)ARG1(arg)));
                    442:     use_unix = 1;
                    443:   }
                    444:   if ( !ARG3(arg) || NUM(ARG3(arg)) ) {
                    445:     if ( use_unix )
                    446:       error("register_server : the protocol should conincide for two sockets");
                    447:     sprintf(sport_str,"%d",QTOS((Q)ARG3(arg)));
                    448:   } else {
                    449:     if ( !use_unix )
                    450:       error("register_server : the protocol should conincide for two sockets");
                    451:     strcpy(sport_str,BDY((STRING)ARG3(arg)));
                    452:   }
                    453:
                    454:   /* client mode */
                    455:   cn = get_iofp(cs,cport_str,0);
                    456:   sn = get_iofp(ss,sport_str,0);
                    457:   /* get_iofp returns -1 if the laucher could not spawn the server */
                    458:   if ( sn < 0 ) {
                    459:     /* we should terminate the launcher */
                    460:     ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
                    461:     STOQ(-1,*rp);
                    462:     return;
                    463:   }
                    464:
                    465:   /* register server to the server list */
                    466:   ind = register_server(use_unix,cn,sn,-1);
                    467:
                    468:   if ( ox_exchange_mathcap ) {
                    469:     /* request remote mathcap */
                    470:     ox_send_cmd(sn,SM_mathcap);
                    471:     ox_send_cmd(sn,SM_popCMO);
                    472:     ox_flush_stream_force(sn);
                    473:     ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
                    474:     store_remote_mathcap(sn,server_mathcap);
                    475:
                    476:     /* send my mathcap */
                    477:     create_my_mathcap("asir");
                    478:     ox_send_data(sn,my_mathcap);
                    479:     ox_send_cmd(sn,SM_setMathcap);
                    480:   }
                    481:   /* return the server id */
                    482:   STOQ(ind,*rp);
1.1       noro      483: }
                    484:
1.70      fujimoto  485: #if !defined(VISUAL) && !defined(__MINGW32__)
1.55      noro      486: #include <sys/file.h>
                    487: #include <sys/types.h>
                    488: #include <sys/stat.h>
                    489: #include <pwd.h>
                    490:
                    491: static int find_executable(char *);
                    492: static int find_executable_main(char *);
                    493:
                    494: static int find_executable(char *com)
                    495: {
1.76      noro      496:   char *c,*s;
                    497:   int len;
                    498:   char dir[BUFSIZ],path[BUFSIZ];
                    499:
                    500:   for ( s = (char *)getenv("PATH"); s; ) {
                    501:     c = (char *)index(s,':');
                    502:     if ( c ) {
                    503:       len = c-s;
                    504:       strncpy(dir,s,len); s = c+1; dir[len] = 0;
                    505:     } else {
                    506:       strcpy(dir,s); s = 0;
                    507:     }
                    508:     sprintf(path,"%s/%s",dir,com);
                    509:     if ( find_executable_main(path) )
                    510:       return 1;
                    511:   }
                    512:   return 0;
1.55      noro      513: }
                    514:
                    515: static int find_executable_main(char *file)
                    516: {
1.76      noro      517:   struct stat buf;
1.55      noro      518:
1.76      noro      519:   if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
                    520:     return 0;
                    521:   if ( access(file,X_OK) )
                    522:     return 0;
                    523:   else
                    524:     return 1;
1.55      noro      525: }
                    526:
                    527: #endif
1.1       noro      528: /*
                    529:   ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
                    530:
                    531:   Input
1.76      noro      532:   host: hostname on which servers run
                    533:   launcher: path name of the launcher
                    534:   server: path name of the server
                    535:   use_unix: use UNIX domain socket if 1
                    536:   use_ssh: use ssh if 1
                    537:   use_x: use X11 facilities if 1
                    538:   conn_to_serv: connect to server if 1
1.1       noro      539: */
                    540:
1.26      noro      541: void Pox_launch_generic(NODE arg,Q *rp)
1.1       noro      542: {
1.76      noro      543:   int use_unix,use_ssh,use_x,conn_to_serv;
                    544:   char *host,*launcher,*server;
                    545:   Q ret;
                    546:
                    547:   host = (arg&&ARG0(arg))?BDY((STRING)ARG0(arg)):0;
                    548:   launcher = BDY((STRING)ARG1(arg));
                    549:   server = BDY((STRING)ARG2(arg));
                    550:   use_unix = !IS_CYGWIN && ARG3(arg) ? 1 : 0;
                    551:   use_ssh = ARG4(arg) ? 1 : 0;
                    552:   use_x = ARG5(arg) ? 1 : 0;
                    553:   conn_to_serv = QTOS((Q)ARG6(arg));
                    554:   if ( !IS_CYGWIN && !host )
                    555:     use_unix = 1;
                    556:   ox_launch_generic(host,launcher,server,
                    557:     use_unix,use_ssh,use_x,conn_to_serv,&ret);
                    558:   *rp = ret;
1.1       noro      559: }
1.56      noro      560:
                    561: #if 0
                    562: void ox_launcher_101_generic(char *host,char *launcher,
1.76      noro      563:     int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
1.56      noro      564: {
1.76      noro      565:   int cs,cn,ind,id;
                    566:   char control_port_str[BUFSIZ];
                    567:   Obj obj;
1.56      noro      568:
1.70      fujimoto  569: #if !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro      570:   if ( use_unix && !find_executable("xterm") ) use_x = 0;
1.56      noro      571: #endif
1.76      noro      572:   control_port_str[0] = 0;
                    573:   do {
                    574:     generate_port(use_unix,control_port_str);
                    575:     if ( conn_to_serv ) {
                    576:       spawn_server_101(host,launcher,
                    577:         use_unix,use_ssh,use_x,conn_to_serv,
                    578:           control_port_str);
                    579:       cs = try_connect(use_unix,host,control_port_str);
                    580:     } else {
                    581:       cs = try_bind_listen(use_unix,control_port_str);
                    582:       if ( cs < 0 ) continue;
                    583:       spawn_laucher_101(host,launcher,
                    584:         use_unix,use_ssh,use_x,conn_to_serv,
                    585:           control_port_str);
                    586:       cs = try_accept(use_unix,cs);
                    587:     }
                    588:   } while ( cs < 0 );
1.56      noro      589:
1.76      noro      590:   /* client mode */
                    591:   cn = get_iofp(cs,control_port_str,0);
1.56      noro      592:
1.76      noro      593:   /* register server to the server list */
                    594:   ind = register_server_101(use_unix,cn);
1.56      noro      595:
1.76      noro      596:   STOQ(ind,*rp);
1.56      noro      597: }
                    598: #endif
1.1       noro      599:
1.26      noro      600: void ox_launch_generic(char *host,char *launcher,char *server,
1.76      noro      601:     int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
1.1       noro      602: {
1.76      noro      603:   int cs,ss,cn,sn,ind,id;
                    604:   char control_port_str[BUFSIZ];
                    605:   char server_port_str[BUFSIZ];
                    606:   Obj obj;
                    607:   MATHCAP server_mathcap;
1.59      ohara     608:     Q value;
                    609:     char *key;
                    610:     int fd=-1;
                    611:     NODE opt,n0;
                    612:
                    613:     if ( current_option ) {
                    614:         for ( opt = current_option; opt; opt = NEXT(opt) ) {
                    615:             n0 = BDY((LIST)BDY(opt));
                    616:             key = BDY((STRING)BDY(n0));
                    617:             value = (Q)BDY(NEXT(n0));
                    618:             if ( !strcmp(key,"fd") && value ) {
                    619:                 fd = QTOS(value);
                    620:                 break;
                    621:             }
                    622:         }
                    623:     }
                    624:     if (!available_mcindex(fd)) {
                    625:         STOQ(-1,*rp);
                    626:         return;
                    627:     }
1.70      fujimoto  628: #if !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro      629:   if ( use_unix && !find_executable("xterm") ) use_x = 0;
1.55      noro      630: #endif
1.76      noro      631:   control_port_str[0] = 0;
                    632:   server_port_str[0] = 0;
                    633:   do {
                    634:     generate_port(use_unix,control_port_str);
                    635:     generate_port(use_unix,server_port_str);
                    636:     if ( !conn_to_serv ) {
                    637:       cs = try_bind_listen(use_unix,control_port_str);
                    638:       if ( cs < 0 ) continue;
                    639:       ss = try_bind_listen(use_unix,server_port_str);
                    640:       if ( ss < 0 ) continue;
                    641:     }
                    642:     spawn_server(host,launcher,server,
                    643:       use_unix,use_ssh,use_x,conn_to_serv,
                    644:         control_port_str,server_port_str);
                    645:     if ( conn_to_serv ) {
                    646:       cs = try_connect(use_unix,host,control_port_str);
                    647:       if ( cs < 0 ) continue;
                    648:       ss = try_connect(use_unix,host,server_port_str);
                    649:       if ( ss < 0 ) continue;
                    650:     } else {
                    651:       cs = try_accept(use_unix,cs);
                    652:       if ( cs < 0 ) continue;
                    653:       ss = try_accept(use_unix,ss);
                    654:       if ( ss < 0 ) continue;
                    655:     }
                    656:   } while ( cs < 0 || ss < 0 );
                    657:
                    658:   /* client mode */
                    659:   cn = get_iofp(cs,control_port_str,0);
                    660:   sn = get_iofp(ss,server_port_str,0);
                    661:   /* get_iofp returns -1 if the laucher could not spawn the server */
                    662:   if ( sn < 0 ) {
                    663:     /* we should terminate the launcher */
                    664:     ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
                    665:     STOQ(-1,*rp);
                    666:     return;
                    667:   }
                    668:
                    669:   /* register server to the server list */
                    670:   ind = register_server(use_unix,cn,sn,fd);
                    671:
                    672:   if ( ox_exchange_mathcap ) {
                    673:     /* request remote mathcap */
                    674:     ox_send_cmd(sn,SM_mathcap);
                    675:     ox_send_cmd(sn,SM_popCMO);
                    676:     ox_flush_stream_force(sn);
                    677:     ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
                    678:     store_remote_mathcap(sn,server_mathcap);
                    679:
                    680:     /* send my mathcap */
                    681:     create_my_mathcap("asir");
                    682:     ox_send_data(sn,my_mathcap);
                    683:     ox_send_cmd(sn,SM_setMathcap);
                    684:   }
                    685:   /* return the server id */
                    686:   STOQ(ind,*rp);
1.1       noro      687: }
                    688:
1.67      fujimoto  689: #if defined(__CYGWIN32__)
1.29      noro      690: static void bslash2slash(char *buf)
                    691: {
1.76      noro      692:   char *p;
1.29      noro      693:
1.76      noro      694:   for ( p = buf; *p; p++ )
                    695:     if ( *p == '\\' )
                    696:       *p = '/';
1.29      noro      697: }
                    698:
1.28      noro      699: static int get_start_path(char *buf)
                    700: {
1.76      noro      701:   static char start_path[BUFSIZ];
                    702:   static int start_initialized = 0;
                    703:   char name[BUFSIZ];
                    704:
                    705:   if ( start_initialized ) {
                    706:     strcpy(buf,start_path);
                    707:     return 1;
                    708:   }
                    709:
                    710:   /* Windows98 */
                    711:   strcpy(buf,"c:\\windows\\command\\start.exe");
                    712:   cygwin_conv_to_full_posix_path(buf,name);
                    713:   if ( !access(name,X_OK) ) {
                    714:     bslash2slash(buf);
                    715:     strcpy(start_path,buf);
                    716:     start_initialized  = 1;
                    717:     return 1;
                    718:   }
                    719:
                    720:   /* Windows2000 */
                    721:   strcpy(buf,"c:\\winnt\\system32\\start.exe");
                    722:   cygwin_conv_to_full_posix_path(buf,name);
                    723:   if ( !access(name,X_OK) ) {
                    724:     bslash2slash(buf);
                    725:     strcpy(start_path,buf);
                    726:     start_initialized  = 1;
                    727:     return 1;
                    728:   }
                    729:
                    730:   strcpy(buf,"c:\\winnt\\system32\\cmd.exe");
                    731:   cygwin_conv_to_full_posix_path(buf,name);
                    732:   if ( !access(name,X_OK) ) {
                    733:     bslash2slash(buf);
                    734:     sprintf(start_path,"%s /c start",buf);
                    735:     strcpy(buf,start_path);
                    736:     start_initialized  = 1;
                    737:     return 1;
                    738:   }
                    739:
                    740:   strcpy(buf,"c:\\windows\\system32\\cmd.exe");
                    741:   cygwin_conv_to_full_posix_path(buf,name);
                    742:   if ( !access(name,X_OK) ) {
                    743:     bslash2slash(buf);
                    744:     sprintf(start_path,"%s /c start",buf);
                    745:     strcpy(buf,start_path);
                    746:     start_initialized  = 1;
                    747:     return 1;
                    748:   }
1.28      noro      749:
1.76      noro      750:   return 0;
1.28      noro      751: }
                    752:
                    753: static void get_launcher_path(char *buf)
                    754: {
1.76      noro      755:   static char rootname[BUFSIZ];
                    756:   static char launcher_path[BUFSIZ];
                    757:   static int launcher_initialized = 0;
                    758:   char name[BUFSIZ];
                    759:
                    760:   if ( launcher_initialized ) {
                    761:     strcpy(buf,launcher_path);
                    762:     return;
                    763:   }
                    764:
                    765:   get_rootdir(rootname,sizeof(rootname));
                    766:   sprintf(name,"%s/ox_launch.exe",rootname);
                    767:   cygwin_conv_to_full_win32_path(name,launcher_path);
                    768:   bslash2slash(launcher_path);
                    769:   launcher_initialized = 1;
                    770:   strcpy(buf,launcher_path);
1.28      noro      771: }
                    772: #endif
                    773:
1.26      noro      774: void spawn_server(char *host,char *launcher,char *server,
1.76      noro      775:   int use_unix,int use_ssh,int use_x,int conn_to_serv,
                    776:   char *control_port_str,char *server_port_str)
1.1       noro      777: {
1.76      noro      778:   char localhost[BUFSIZ];
                    779:   char *dname,*conn_str,*rsh,*dname0,*asirhost;
                    780:   char AsirExe[BUFSIZ];
                    781:   STRING rootdir;
                    782:   char prog[BUFSIZ];
                    783:   char *av[BUFSIZ];
1.70      fujimoto  784: #if !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro      785:   char cmd[BUFSIZ];
1.26      noro      786: #endif
1.28      noro      787: #if defined(__CYGWIN__)
1.76      noro      788:   char win_start[BUFSIZ],win_launcher[BUFSIZ];
1.28      noro      789: #endif
1.76      noro      790:   void Pget_rootdir();
1.1       noro      791:
1.76      noro      792:   dname0 = (char *)getenv("DISPLAY");
                    793:   if ( !dname0 )
                    794:     dname0 = "0";
                    795:   dname = use_x ? dname0 : 0;
                    796:   conn_str = conn_to_serv ? "1" : "0";
                    797:   rsh = getenv("ASIR_RSH");
                    798:   if ( !rsh )
                    799:     rsh = use_ssh ? "ssh" : RSH;
                    800:   if ( !use_unix && strstr(rsh,"ssh") ) {
                    801:     /*
                    802:      * if "ssh" is used to invoke a remote server,
                    803:      * we should not specify "-display".
                    804:      */
                    805:     use_ssh = 1;
                    806:   }
                    807:   asirhost = (char *)getenv("ASIRHOSTNAME");
                    808:   if ( asirhost )
                    809:     strcpy(localhost,asirhost);
                    810:   else
                    811:     gethostname(localhost,BUFSIZ);
1.70      fujimoto  812: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro      813:   if ( !use_unix )
                    814:     error("spawn_server : not implemented on Windows");
                    815:   Pget_rootdir(&rootdir);
                    816:   sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
                    817:   strcpy(prog,server);
                    818:   server = strrchr(prog,'/')+1;
                    819:   av[0] = "ox_launch";
                    820:   av[1] = "127.0.0.1";
                    821:   av[2] = conn_str;
                    822:   av[3] = control_port_str;
                    823:   av[4] = server_port_str;
                    824:   av[5] = server;
                    825:   av[6] = use_x ? "1" : "0";
                    826:   av[7] = 0;
                    827:
                    828:   _spawnv(_P_NOWAIT,AsirExe,av);
                    829: //  _spawnv(_P_NOWAIT,"d:\\home\\noro\\engine2000\\debug\\engine.exe",av);
                    830: //  printf("ox_launch 127.0.0.1 %s %s %s %s 0\n",conn_str,control_port_str,server_port_str,server);
1.15      noro      831: #else
1.76      noro      832:   if ( use_unix || !host ) {
1.67      fujimoto  833: #if defined(__CYGWIN32__)
1.76      noro      834:     get_launcher_path(win_launcher);
                    835:     if ( dname && strchr(dname,':') ) {
                    836:       if ( !fork() ) {
                    837:         setpgid(0,getpid());
                    838:         execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
                    839:           "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
                    840:           control_port_str,server_port_str,server,dname,(char *)0);
                    841:       }
                    842:     } else if ( dname && get_start_path(win_start) ) {
                    843:       sprintf(cmd,"%s %s %s %s %s %s %s 1",
                    844:         win_start,win_launcher,use_unix?".":"127.1",conn_str,
                    845:         control_port_str,server_port_str,server);
                    846:       system(cmd);
                    847:     } else {
                    848:       if ( !fork() ) {
                    849:         setpgid(0,getpid());
                    850:         execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
                    851:           control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
                    852:       }
                    853:     }
1.27      noro      854: #else
1.76      noro      855:     if ( !fork() ) {
                    856:       setpgid(0,getpid());
                    857:       if ( dname )
                    858:         execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
                    859:           "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
                    860:           control_port_str,server_port_str,server,dname,(char *)0);
                    861:       else
                    862:         execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
                    863:           control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
                    864:     }
                    865: #endif
                    866:   } else if ( conn_to_serv == 2 ) {
                    867:     /* special support for java */
                    868:     if ( dname )
                    869:       sprintf(cmd,
                    870:         "%s -n %s \"(cd %s; xterm -name %s %s -geometry 60x10 -e java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
                    871:         rsh,host,launcher,OX_XTERM,dname,server,localhost,control_port_str,server_port_str);
                    872:     else
                    873:       sprintf(cmd,
                    874:         "%s -n %s \"(cd %s; java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
                    875:         rsh,host,launcher,server,localhost,
                    876:         control_port_str,server_port_str,server);
                    877:     fprintf(stderr,"%s\n",cmd);
                    878:     sleep(20);
                    879: /*    system(cmd); */
                    880:   } else {
                    881:     if ( dname )
                    882:       if ( use_ssh )
                    883:         sprintf(cmd,
1.51      noro      884: "%s -f -n %s \"xterm -name %s -title ox_launch:%s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null\">/dev/null",
1.76      noro      885:         rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
                    886:         control_port_str,server_port_str,server,"1");
                    887:       else
                    888:         sprintf(cmd,
1.51      noro      889: "%s -n %s \"xterm -name %s -title ox_launch:%s -display %s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null&\">/dev/null",
1.76      noro      890:         rsh,host,OX_XTERM,host,dname,launcher,localhost,conn_str,
                    891:         control_port_str,server_port_str,server,dname);
                    892:     else
                    893:       if ( use_ssh )
                    894:         sprintf(cmd,
1.25      noro      895: "%s -f -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
1.76      noro      896:         rsh,host,launcher,localhost,conn_str,
                    897:         control_port_str,server_port_str,server,"1","-nolog");
                    898:       else
                    899:         sprintf(cmd,
1.17      noro      900: "%s -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
1.76      noro      901:         rsh,host,launcher,localhost,conn_str,
                    902:         control_port_str,server_port_str,server,dname0,"-nolog");
                    903:     system(cmd);
                    904:   }
1.15      noro      905: #endif /* VISUAL */
1.1       noro      906: }
                    907:
1.26      noro      908: void Pox_launch(NODE arg,Obj *rp)
1.1       noro      909: {
1.76      noro      910:   ox_launch_main(1,arg,rp);
1.1       noro      911: }
                    912:
1.26      noro      913: void Pox_launch_nox(NODE arg,Obj *rp)
1.1       noro      914: {
1.76      noro      915:   ox_launch_main(0,arg,rp);
1.1       noro      916: }
                    917:
                    918: /*
1.76      noro      919:   ox_launch() : invoke local ox_asir
                    920:   ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
                    921:   ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
1.1       noro      922: */
                    923:
1.26      noro      924: void ox_launch_main(int with_x,NODE arg,Obj *p)
1.1       noro      925: {
1.76      noro      926:   char *str;
                    927:   char *hostname,*servername;
                    928:   char *control;
                    929:   int use_unix;
                    930:   Q ret;
                    931:   extern char *asir_libdir;
                    932:
                    933:   if ( arg && ARG0(arg) && argc(arg) != 3 )
                    934:     error("ox_launch : argument mismatch");
                    935:   control = (char *)MALLOC(BUFSIZ);
                    936:   if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
                    937:     sprintf(control,"%s/ox_launch",asir_libdir);
                    938:     use_unix = IS_CYGWIN ? 0 : 1;
                    939:     servername = (char *)MALLOC(BUFSIZ);
                    940:     sprintf(servername,"%s/ox_asir",asir_libdir);
                    941:   } else if ( !ARG0(arg) && argc(arg) == 2 ) {
                    942:     sprintf(control,"%s/ox_launch",asir_libdir);
                    943:     use_unix = IS_CYGWIN ? 0 : 1;
                    944:     str = BDY((STRING)ARG1(arg));
                    945:     if ( str[0] == '/' )
                    946:       servername = str;
                    947:     else {
                    948:       servername = (char *)MALLOC(BUFSIZ);
                    949:       sprintf(servername,"%s/%s",asir_libdir,str);
                    950:     }
                    951:   } else {
                    952:     sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
                    953:     if ( !ARG0(arg) )
                    954:       use_unix = IS_CYGWIN ? 0 : 1;
                    955:     else
                    956:       use_unix = 0;
                    957:     str = BDY((STRING)ARG2(arg));
                    958:     if ( str[0] == '/' )
                    959:       servername = str;
                    960:     else {
                    961:       servername = (char *)MALLOC(BUFSIZ);
                    962:       sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
                    963:     }
                    964:   }
                    965:   if ( arg && ARG0(arg) )
                    966:     hostname = BDY((STRING)ARG0(arg));
                    967:   else
                    968:     hostname = 0;
                    969:   ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,&ret);
                    970:   *p = (Obj)ret;
1.1       noro      971: }
                    972:
1.59      ohara     973: void extend_mctab(int bound)
                    974: {
1.76      noro      975:   int i,n;
                    976:   struct m_c *t;
1.59      ohara     977:     if ( !m_c_tab ) {
                    978:         n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
                    979:         t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
                    980:         for ( i = m_c_s; i < n; i++ ) {
                    981:             t[i].af_unix = 0;
                    982:             t[i].m = t[i].c = -1;
                    983:         }
                    984:         m_c_s = n; m_c_tab = t;
                    985:     }else if (bound >= m_c_s) {
                    986:         n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
                    987:         t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
1.65      ohara     988:         bzero((void *)t,n*sizeof(struct m_c));
1.59      ohara     989:         bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
                    990:         for ( i = m_c_s; i < n; i++ ) {
                    991:             t[i].af_unix = 0;
                    992:             t[i].m = t[i].c = -1;
                    993:         }
                    994:         m_c_s = n; m_c_tab = t;
                    995:     }else {
                    996:         return;
                    997:     }
                    998: }
                    999:
                   1000: int available_mcindex(int ind)
                   1001: {
1.76      noro     1002:   if (ind < 0) return 1;
                   1003:   extend_mctab(ind);
                   1004:   return m_c_tab[ind].m<0 && m_c_tab[ind].c<0;
1.59      ohara    1005: }
                   1006:
                   1007: int register_server(int af_unix,int m,int c,int ind)
1.1       noro     1008: {
1.76      noro     1009:   int s,i;
                   1010:   struct m_c *t;
                   1011:   if ( c < 0 )
                   1012:     return -1;
                   1013:   extend_mctab( (ind<0)? 0: ind );
                   1014:   if(ind >= 0) {
                   1015:     if (m_c_tab[ind].m<0 && m_c_tab[ind].c<0) {
                   1016:       m_c_tab[ind].m = m; m_c_tab[ind].c = c;
                   1017:       m_c_tab[ind].af_unix = af_unix;
                   1018:       if (ind>=m_c_i) m_c_i = ind+1;
                   1019:       return ind;
                   1020:     }
                   1021:     return -1;
                   1022:   }
1.39      noro     1023: #if !defined(MPI)
1.76      noro     1024:   for ( i = 0; i < m_c_i; i++ )
                   1025:     if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
                   1026:       break;
                   1027:   if ( i < m_c_i ) {
                   1028:     m_c_tab[i].m = m; m_c_tab[i].c = c;
                   1029:     m_c_tab[i].af_unix = af_unix;
                   1030:     return i;
                   1031:   }
                   1032: #endif
                   1033:   if ( m_c_i == m_c_s ) {
                   1034:     s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
                   1035:     t = (struct m_c *)MALLOC_ATOMIC(s);
                   1036:     bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
                   1037:     for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
                   1038:       t[m_c_s+i].af_unix = 0;
                   1039:       t[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
                   1040:     }
                   1041:     m_c_s += INIT_TAB_SIZ; m_c_tab = t;
                   1042:   }
                   1043:   m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
                   1044:   m_c_tab[m_c_i].af_unix = af_unix;
                   1045:   return m_c_i++;
1.1       noro     1046: }
                   1047:
                   1048: /* iofp index => m_c_tab index */
                   1049:
1.26      noro     1050: int get_mcindex(int i)
1.1       noro     1051: {
1.76      noro     1052:   int j;
1.1       noro     1053:
1.76      noro     1054:   for ( j = 0; j < m_c_i; j++ )
                   1055:     if ( m_c_tab[j].c == i )
                   1056:       return j;
                   1057:   return -1;
1.1       noro     1058: }
                   1059:
                   1060: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
                   1061:
1.26      noro     1062: void Pox_select(NODE arg,LIST *rp)
1.1       noro     1063: {
1.76      noro     1064:   int fd,n,i,index,mcind,s,maxfd,minfd;
                   1065:   fd_set r,w,e;
                   1066:   NODE list,t,t1,t0;
                   1067:   Q q;
                   1068:   double max;
                   1069:   struct timeval interval;
                   1070:   struct timeval *tvp;
                   1071:
                   1072:   list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
                   1073:   if ( arg ) {
                   1074:     max = ToReal((Num)BDY(arg));
                   1075:     interval.tv_sec = (int)max;
                   1076:     interval.tv_usec = (int)((max-(int)max)*1000000);
                   1077:     tvp = &interval;
                   1078:   } else
                   1079:     tvp = 0;
                   1080:
                   1081:   FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
                   1082:   maxfd = minfd = -1;
                   1083:   for ( t = list, t0 = 0; t; t = NEXT(t) ) {
                   1084:     index = QTOS((Q)BDY(t));
                   1085:     valid_mctab_index(index);
                   1086:     s = m_c_tab[index].c;
                   1087:     if ( ox_data_is_available(s) ) {
                   1088:       MKNODE(t1,(Q)BDY(t),t0); t0 = t1;
                   1089:     } else {
                   1090:       fd = get_fd(s); FD_SET((unsigned int)fd,&r);
                   1091:       maxfd = maxfd<0 ? fd : MAX(fd,maxfd);
                   1092:       minfd = minfd<0 ? fd : MIN(fd,minfd);
                   1093:     }
                   1094:   }
                   1095:   if ( t0 ) {
                   1096:     MKLIST(*rp,t0); return;
                   1097:   }
1.57      noro     1098:
1.76      noro     1099:   n = select(FD_SETSIZE,&r,&w,&e,tvp);
1.70      fujimoto 1100: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro     1101:   for ( i = minfd, t = 0; n && i <= maxfd; i++ )
1.60      noro     1102: #else
1.76      noro     1103:   for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
1.60      noro     1104: #endif
1.76      noro     1105:     if ( FD_ISSET(i,&r) ) {
                   1106:       /* index : index to iofp array */
                   1107:       index = get_index(i);
                   1108:       /* mcind : index to m_c_tab array */
                   1109:       mcind = get_mcindex(index);
                   1110:       n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
                   1111:     }
                   1112:   MKLIST(*rp,t);
1.1       noro     1113: }
                   1114:
1.26      noro     1115: void Pox_flush(NODE arg,Q *rp)
1.1       noro     1116: {
1.76      noro     1117:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1118:
1.76      noro     1119:   valid_mctab_index(index);
                   1120:   ox_flush_stream_force(m_c_tab[index].c);
                   1121:   *rp = ONE;
1.1       noro     1122: }
                   1123:
1.26      noro     1124: void Pox_send_raw_cmo(NODE arg,Obj *rp)
1.8       noro     1125: {
1.76      noro     1126:   int s;
                   1127:   int index = QTOS((Q)ARG0(arg));
1.8       noro     1128:
1.76      noro     1129:   valid_mctab_index(index);
                   1130:   s = m_c_tab[index].c;
                   1131:   ox_write_cmo(s,(Obj)ARG1(arg));
                   1132:   /* flush always */
                   1133:   ox_flush_stream(s);
                   1134:   *rp = 0;
1.8       noro     1135: }
                   1136:
1.26      noro     1137: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
1.8       noro     1138: {
1.76      noro     1139:   int s;
                   1140:   int index = QTOS((Q)ARG0(arg));
1.8       noro     1141:
1.76      noro     1142:   valid_mctab_index(index);
                   1143:   s = m_c_tab[index].c;
                   1144:   ox_read_cmo(s,rp);
1.8       noro     1145: }
                   1146:
1.44      noro     1147: void Pox_send_102(NODE arg,Obj *rp)
1.43      noro     1148: {
1.76      noro     1149:   int rank = QTOS((Q)ARG0(arg));
1.43      noro     1150:
1.76      noro     1151:   ox_send_data_102(rank,(Obj)ARG1(arg));
                   1152:   *rp = 0;
1.43      noro     1153: }
                   1154:
1.44      noro     1155: void Pox_recv_102(NODE arg,Obj *rp)
1.43      noro     1156: {
1.76      noro     1157:   int id;
                   1158:   int rank = QTOS((Q)ARG0(arg));
1.43      noro     1159:
1.76      noro     1160:   ox_recv_102(rank,&id,rp);
1.46      noro     1161: }
                   1162:
                   1163: void Pox_bcast_102(NODE arg,Obj *rp)
                   1164: {
1.76      noro     1165:   int rank = QTOS((Q)ARG0(arg));
                   1166:   Obj data;
1.46      noro     1167:
1.76      noro     1168:   if ( argc(arg) > 1 )
                   1169:     asir_push_one((Obj)ARG1(arg));
                   1170:   ox_bcast_102(rank);
                   1171:   *rp = (Obj)asir_pop_one();
1.47      noro     1172: }
                   1173:
                   1174: void Pox_reduce_102(NODE arg,Obj *rp)
                   1175: {
1.76      noro     1176:   int root = QTOS((Q)ARG0(arg));
                   1177:   STRING op;
                   1178:   char *opname;
                   1179:   void (*func)();
                   1180:
                   1181:   op = (STRING)ARG1(arg);
                   1182:   asir_assert(op,O_STR,"ox_reduce_102");
                   1183:   opname = BDY(op);
                   1184:   if ( !strcmp(opname,"+") )
                   1185:     func = arf_add;
                   1186:   else if ( !strcmp(opname,"*") )
                   1187:     func = arf_mul;
                   1188:   else {
                   1189:     error("ox_reduce_102 : operation not supported");
                   1190:   }
                   1191:   if ( argc(arg) > 2 )
                   1192:     asir_push_one((Obj)ARG2(arg));
                   1193:   ox_reduce_102(root,func);
                   1194:   *rp = (Obj)asir_pop_one();
1.43      noro     1195: }
                   1196:
1.26      noro     1197: void Pox_push_local(NODE arg,Obj *rp)
1.1       noro     1198: {
1.76      noro     1199:   int s;
                   1200:   struct oLIST dummy;
                   1201:   VL vl;
                   1202:   int index;
                   1203:
                   1204:   if ( !arg )
                   1205:     error("ox_push_local : too few arguments.");
                   1206:   index = QTOS((Q)ARG0(arg));
                   1207:   valid_mctab_index(index);
                   1208:   s = m_c_tab[index].c; arg = NEXT(arg);
                   1209:
                   1210:   dummy.id = O_LIST; dummy.body = arg;
                   1211:   get_vars_recursive((Obj)&dummy,&vl);
                   1212:
                   1213:   ox_send_local_ring(s,vl);
                   1214:   for ( ; arg; arg = NEXT(arg) )
                   1215:     ox_send_local_data(s,BDY(arg));
                   1216:   *rp = 0;
1.1       noro     1217: }
                   1218:
1.26      noro     1219: void Pox_push_cmo(NODE arg,Obj *rp)
1.1       noro     1220: {
1.76      noro     1221:   int s;
                   1222:   int index;
1.1       noro     1223:
1.76      noro     1224:   if ( !arg )
                   1225:     error("ox_push_cmo : too few arguments.");
                   1226:   index = QTOS((Q)ARG0(arg));
                   1227:   valid_mctab_index(index);
                   1228:   s = m_c_tab[index].c; arg = NEXT(arg);
                   1229:   for ( ; arg; arg = NEXT(arg) )
                   1230:     ox_send_data(s,BDY(arg));
                   1231:   *rp = 0;
1.1       noro     1232: }
                   1233:
1.26      noro     1234: void Pox_push_vl(NODE arg,Obj *rp)
1.1       noro     1235: {
1.76      noro     1236:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1237:
1.76      noro     1238:   valid_mctab_index(index);
                   1239:   ox_send_local_ring(m_c_tab[index].c,CO);
                   1240:   *rp = 0;
1.1       noro     1241: }
                   1242:
1.26      noro     1243: void Pox_pop_local(NODE arg,Obj *rp)
1.1       noro     1244: {
1.76      noro     1245:   int s;
                   1246:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1247:
1.76      noro     1248:   valid_mctab_index(index);
                   1249:   s = m_c_tab[index].c;
                   1250:   ox_send_cmd(s,SM_popSerializedLocalObject);
                   1251:   ox_flush_stream_force(s);
                   1252:   ox_get_result(s,rp);
1.1       noro     1253: }
                   1254:
1.26      noro     1255: void Pox_pop_cmo(NODE arg,Obj *rp)
1.1       noro     1256: {
1.76      noro     1257:   int s;
                   1258:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1259:
1.76      noro     1260:   valid_mctab_index(index);
                   1261:   s = m_c_tab[index].c;
                   1262:   ox_send_cmd(s,SM_popCMO);
                   1263:   ox_flush_stream_force(s);
                   1264:   ox_get_result(s,rp);
1.1       noro     1265: }
                   1266:
1.26      noro     1267: void Pox_pop0_local(NODE arg,Obj *rp)
1.1       noro     1268: {
1.76      noro     1269:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1270:
1.76      noro     1271:   valid_mctab_index(index);
                   1272:   ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
                   1273:   *rp = 0;
1.1       noro     1274: }
                   1275:
1.26      noro     1276: void Pox_pop0_cmo(NODE arg,Obj *rp)
1.1       noro     1277: {
1.76      noro     1278:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1279:
1.76      noro     1280:   valid_mctab_index(index);
                   1281:   ox_send_cmd(m_c_tab[index].c,SM_popCMO);
                   1282:   *rp = 0;
1.1       noro     1283: }
                   1284:
1.26      noro     1285: void Pox_pop0_string(NODE arg,STRING *rp)
1.1       noro     1286: {
1.76      noro     1287:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1288:
1.76      noro     1289:   valid_mctab_index(index);
                   1290:   ox_send_cmd(m_c_tab[index].c,SM_popString);
                   1291:   *rp = 0;
1.1       noro     1292: }
                   1293:
1.26      noro     1294: void Pox_pop_string(NODE arg,Obj *rp)
1.1       noro     1295: {
1.76      noro     1296:   int s;
                   1297:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1298:
1.76      noro     1299:   valid_mctab_index(index);
                   1300:   s = m_c_tab[index].c;
                   1301:   ox_send_cmd(s,SM_popString);
                   1302:   ox_flush_stream_force(s);
                   1303:   ox_get_result(s,rp);
1.1       noro     1304: }
                   1305:
1.26      noro     1306: void Pox_get(NODE arg,Obj *rp)
1.1       noro     1307: {
1.76      noro     1308:   int index;
                   1309:   int s;
1.1       noro     1310:
1.76      noro     1311:   if ( !arg ) {
                   1312:     /* client->server */
                   1313:     ox_get_result(0,rp);
                   1314:   } else {
                   1315:     /* server->client */
                   1316:     index = QTOS((Q)ARG0(arg));
                   1317:     valid_mctab_index(index);
                   1318:     s = m_c_tab[index].c;
                   1319:     ox_flush_stream_force(s);
                   1320:     ox_get_result(s,rp);
                   1321:   }
1.1       noro     1322: }
                   1323:
1.26      noro     1324: void Pox_pops(NODE arg,Obj *rp)
1.1       noro     1325: {
1.76      noro     1326:   int s;
                   1327:   USINT n;
                   1328:   int index = QTOS((Q)ARG0(arg));
                   1329:
                   1330:   valid_mctab_index(index);
                   1331:   s = m_c_tab[index].c;
                   1332:   if ( NEXT(arg) )
                   1333:     MKUSINT(n,QTOS((Q)ARG1(arg)));
                   1334:   else
                   1335:     MKUSINT(n,1);
                   1336:   ox_send_data(s,n);
                   1337:   ox_send_cmd(s,SM_pops);
                   1338:   *rp = 0;
1.1       noro     1339: }
                   1340:
1.26      noro     1341: void Pox_execute_function(NODE arg,Obj *rp)
1.1       noro     1342: {
1.76      noro     1343:   int s;
                   1344:   USINT ui;
                   1345:   int index = QTOS((Q)ARG0(arg));
                   1346:
                   1347:   valid_mctab_index(index);
                   1348:   s = m_c_tab[index].c;
                   1349:   MKUSINT(ui,QTOS((Q)ARG2(arg)));
                   1350:   ox_send_data(s,ui);
                   1351:   ox_send_data(s,ARG1(arg));
                   1352:   ox_send_cmd(s,SM_executeFunction);
                   1353:   *rp = 0;
1.1       noro     1354: }
                   1355:
1.26      noro     1356: void Pox_setname(NODE arg,Obj *rp)
1.1       noro     1357: {
1.76      noro     1358:   int s;
                   1359:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1360:
1.76      noro     1361:   valid_mctab_index(index);
                   1362:   s = m_c_tab[index].c;
                   1363:   ox_send_data(s,ARG1(arg));
                   1364:   ox_send_cmd(s,SM_setName);
                   1365:   *rp = 0;
1.1       noro     1366: }
                   1367:
1.26      noro     1368: void Pox_evalname(NODE arg,Obj *rp)
1.1       noro     1369: {
1.76      noro     1370:   int s;
                   1371:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1372:
1.76      noro     1373:   valid_mctab_index(index);
                   1374:   s = m_c_tab[index].c;
                   1375:   ox_send_data(s,ARG1(arg));
                   1376:   ox_send_cmd(s,SM_evalName);
                   1377:   *rp = 0;
1.1       noro     1378: }
                   1379:
1.26      noro     1380: void Pox_execute_string(NODE arg,Obj *rp)
1.1       noro     1381: {
1.76      noro     1382:   int s;
                   1383:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1384:
1.76      noro     1385:   asir_assert(ARG1(arg),O_STR,"ox_execute_string");
                   1386:   valid_mctab_index(index);
                   1387:   s = m_c_tab[index].c;
                   1388:   ox_send_data(s,ARG1(arg));
                   1389:   ox_send_cmd(s,SM_executeStringByLocalParser);
                   1390:   *rp = 0;
1.1       noro     1391: }
                   1392:
                   1393: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
                   1394:
1.26      noro     1395: void Pox_rpc(NODE arg,Obj *rp)
1.1       noro     1396: {
1.76      noro     1397:   int s,i,n;
                   1398:   STRING f;
                   1399:   USINT ui;
                   1400:   pointer *w;
                   1401:   NODE t;
                   1402:   int index = QTOS((Q)ARG0(arg));
                   1403:
                   1404:   valid_mctab_index(index);
                   1405:   s = m_c_tab[index].c; arg = NEXT(arg);
                   1406:   f = (STRING)BDY(arg); arg = NEXT(arg);
                   1407:   ox_send_local_ring(s,CO);
                   1408:   for ( n = 0, t = arg; t; t = NEXT(t), n++ );
                   1409:   w = (pointer *)ALLOCA(n*sizeof(pointer));
                   1410:   for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
                   1411:     w[i] = BDY(t);
                   1412:   for ( i = n-1; i >= 0; i-- )
                   1413:     ox_send_local_data(s,w[i]);
                   1414:   MKUSINT(ui,n);
                   1415:   ox_send_data(s,ui);
                   1416:   ox_send_data(s,f);
                   1417:   ox_send_cmd(s,SM_executeFunction);
                   1418:   *rp = 0;
1.1       noro     1419: }
                   1420:
1.26      noro     1421: void Pox_cmo_rpc(NODE arg,Obj *rp)
1.1       noro     1422: {
1.76      noro     1423:   int s,i,n;
                   1424:   STRING f;
                   1425:   USINT ui;
                   1426:   NODE t;
                   1427:   Obj dmy;
                   1428:   pointer *w;
                   1429:   int index = QTOS((Q)ARG0(arg));
                   1430:   int sync,find;
                   1431:
                   1432:   find = get_opt("sync",&sync);
                   1433:   valid_mctab_index(index);
                   1434:   s = m_c_tab[index].c; arg = NEXT(arg);
                   1435:   f = (STRING)BDY(arg); arg = NEXT(arg);
                   1436:   for ( n = 0, t = arg; t; t = NEXT(t), n++ );
                   1437:   w = (pointer *)ALLOCA(n*sizeof(pointer));
                   1438:   for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
                   1439:     w[i] = BDY(t);
                   1440:   for ( i = n-1; i >= 0; i-- )
                   1441:     ox_send_data(s,w[i]);
                   1442:   MKUSINT(ui,n);
                   1443:   ox_send_data(s,ui);
                   1444:   ox_send_data(s,f);
                   1445:   if ( find && sync ) {
                   1446:     ox_send_cmd(s,SM_executeFunctionSync);
                   1447:     ox_get_result(s,&dmy);
                   1448:   } else
                   1449:     ox_send_cmd(s,SM_executeFunction);
                   1450:   *rp = 0;
1.1       noro     1451: }
                   1452:
1.72      noro     1453: int No_ox_reset;
1.74      noro     1454: extern Q ox_pari_stream;
                   1455: extern int ox_pari_stream_initialized;
1.72      noro     1456:
1.26      noro     1457: void Pox_reset(NODE arg,Q *rp)
1.1       noro     1458: {
1.76      noro     1459:   USINT t;
                   1460:   int id,c,m;
                   1461:   Obj obj;
1.72      noro     1462:   NODE nd;
                   1463:   Q q;
1.76      noro     1464:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1465:
1.76      noro     1466:   valid_mctab_index(index);
                   1467:   m = m_c_tab[index].m;
                   1468:   c = m_c_tab[index].c;
                   1469:   if ( m >= 0 ) {
1.72      noro     1470:     if ( no_ox_reset(c) == 1 ) {
                   1471:       STOQ(index,q);
                   1472:       nd = mknode(1,q);
                   1473:       switch ( No_ox_reset ) {
                   1474:       case 1:
                   1475:          fprintf(stderr,"The server does not implenent OX reset protocol.\n");
                   1476:          fprintf(stderr,"The server is terminated.\n");
                   1477:          Pox_shutdown(nd,rp);
1.74      noro     1478:          if ( index == QTOS(ox_pari_stream) ) ox_pari_stream_initialized = 0;
1.72      noro     1479:          break;
                   1480:       case 2:
                   1481:          Pox_shutdown(nd,rp);
1.74      noro     1482:          if ( index == QTOS(ox_pari_stream) ) ox_pari_stream_initialized = 0;
1.72      noro     1483:          break;
                   1484:       default:
                   1485:          error("The server does not implement OX reset protocol.");
1.76      noro     1486:          *rp = ONE;
1.72      noro     1487:          break;
                   1488:       }
                   1489:       return;
                   1490:     }
                   1491:
                   1492:     if ( argc(arg) == 1 ) {
1.76      noro     1493:       ox_send_cmd(m,SM_control_reset_connection);
                   1494:       ox_flush_stream_force(m);
1.73      noro     1495: #if 0
                   1496:       /* XXX obsolete */
1.76      noro     1497:       ox_recv(m,&id,&obj); t = (USINT)obj;
1.73      noro     1498: #endif
1.76      noro     1499:     }
                   1500:     *rp = ONE;
1.70      fujimoto 1501: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro     1502:     Sleep(100);
                   1503:     ox_send_cmd(c,SM_nop);
                   1504:     ox_flush_stream_force(c);
                   1505: #endif
                   1506:     while ( 1 ) {
                   1507:       ox_recv(c,&id,&obj);
                   1508:       if ( id == OX_SYNC_BALL )
                   1509:         break;
                   1510:     }
                   1511:     ox_send_sync(c);
                   1512:   } else
                   1513:     *rp = 0;
1.5       noro     1514: }
                   1515:
1.26      noro     1516: void Pox_intr(NODE arg,Q *rp)
1.5       noro     1517: {
1.76      noro     1518:   int m;
                   1519:   int index = QTOS((Q)ARG0(arg));
1.5       noro     1520:
1.76      noro     1521:   valid_mctab_index(index);
                   1522:   m = m_c_tab[index].m;
                   1523:   if ( m >= 0 ) {
                   1524:     if ( argc(arg) == 1 ) {
                   1525:       ox_send_cmd(m,SM_control_intr);
                   1526:       ox_flush_stream_force(m);
                   1527:     }
                   1528:     *rp = ONE;
                   1529:   } else
                   1530:     *rp = 0;
1.1       noro     1531: }
                   1532:
1.26      noro     1533: void Pox_sync(NODE arg,Q *rp)
1.1       noro     1534: {
1.76      noro     1535:   int c;
                   1536:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1537:
1.76      noro     1538:   valid_mctab_index(index);
                   1539:   c = m_c_tab[index].c;
                   1540:   ox_send_sync(c);
                   1541:   *rp = 0;
1.1       noro     1542: }
                   1543:
1.26      noro     1544: void Pox_shutdown(NODE arg,Q *rp)
1.1       noro     1545: {
1.76      noro     1546:   int s;
                   1547:   int index = QTOS((Q)ARG0(arg));
1.70      fujimoto 1548: #if !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro     1549:   int status;
1.26      noro     1550: #endif
1.1       noro     1551:
1.76      noro     1552:   valid_mctab_index(index);
                   1553:   s = m_c_tab[index].m;
                   1554:   ox_send_cmd(s,SM_shutdown);
                   1555:   free_iofp(s);
                   1556:   s = m_c_tab[index].c;
                   1557:   free_iofp(s);
1.70      fujimoto 1558: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro     1559:   if ( m_c_tab[index].af_unix )
                   1560:     wait(&status);
1.1       noro     1561: #endif
1.76      noro     1562:   m_c_tab[index].m = -1; m_c_tab[index].c = -1;
                   1563:   m_c_tab[index].af_unix = 0;
                   1564:   *rp = 0;
1.1       noro     1565: }
                   1566:
1.26      noro     1567: void Pox_push_cmd(NODE arg,Q *rp)
1.1       noro     1568: {
1.76      noro     1569:   int ui;
                   1570:   int index = QTOS((Q)ARG0(arg));
1.1       noro     1571:
1.76      noro     1572:   valid_mctab_index(index);
                   1573:   ui = QTOS((Q)ARG1(arg));
                   1574:   ox_send_cmd(m_c_tab[index].c,ui);
                   1575:   *rp = 0;
1.1       noro     1576: }
1.16      noro     1577:
                   1578: void shutdown_all() {
1.76      noro     1579:   int s;
                   1580:   int i,index;
1.70      fujimoto 1581: #if !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro     1582:   int status;
1.26      noro     1583: #endif
1.16      noro     1584:
1.76      noro     1585:   for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
                   1586:     index = i;
                   1587:     check_valid_mctab_index(index);
                   1588:     if ( index < 0 )
                   1589:       continue;
                   1590:     s = m_c_tab[index].m;
                   1591:     ox_send_cmd(s,SM_shutdown);
1.70      fujimoto 1592: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro     1593:   Sleep(1000);
1.21      noro     1594: #endif
1.76      noro     1595:     free_iofp(s);
                   1596:     s = m_c_tab[index].c;
                   1597:     free_iofp(s);
1.70      fujimoto 1598: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__)
1.76      noro     1599:     if ( m_c_tab[index].af_unix )
                   1600:       wait(&status);
1.16      noro     1601: #endif
1.76      noro     1602:     m_c_tab[index].m = 0; m_c_tab[index].c = 0;
                   1603:     m_c_tab[index].af_unix = 0;
                   1604:   }
1.33      noro     1605: }
                   1606:
                   1607: char *ox_get_servername(int);
                   1608:
                   1609: int is_ox_plot(int index)
                   1610: {
1.76      noro     1611:   char *name;
1.33      noro     1612:
1.76      noro     1613:   check_valid_mctab_index(index);
                   1614:   if ( index < 0 )
                   1615:     return 0;
                   1616:   /* m : client, c : server ??? */
                   1617:   name = ox_get_servername(m_c_tab[index].c);
                   1618:   return strcmp(name,"ox_plot") ? 0 : 1;
1.33      noro     1619: }
                   1620:
1.75      noro     1621: int debug_plot;
                   1622:
1.33      noro     1623: int validate_ox_plot_stream(int index)
                   1624: {
1.76      noro     1625:   int i;
                   1626:   NODE arg;
                   1627:   STRING name;
                   1628:   Obj r;
                   1629:
                   1630:   if ( is_ox_plot(index) )
                   1631:     return index;
                   1632:   for ( i = 0; i < m_c_i; i++ )
                   1633:     if ( is_ox_plot(i) )
                   1634:       return i;
                   1635:
                   1636:   /* create an ox_plot server */
                   1637:   MKSTR(name,"ox_plot");
                   1638:   arg = mknode(2,NULL,name);
1.75      noro     1639:   if ( debug_plot ) Pox_launch(arg,&r);
                   1640:   else Pox_launch_nox(arg,&r);
1.76      noro     1641:   i = QTOS((Q)r);
1.70      fujimoto 1642: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro     1643:   Sleep(100);
                   1644:   ox_send_cmd(m_c_tab[i].c,SM_nop);
                   1645:   ox_flush_stream_force(m_c_tab[i].c);
1.34      noro     1646: #endif
1.76      noro     1647:   return i;
1.42      noro     1648: }
                   1649:
                   1650: int get_ox_server_id(int index)
                   1651: {
1.76      noro     1652:   valid_mctab_index(index);
                   1653:   return m_c_tab[index].c;
1.43      noro     1654: }
                   1655:
                   1656: int register_102(int s1,int rank,int is_master)
                   1657: {
1.76      noro     1658:   unsigned char c,rc;
1.43      noro     1659:
1.76      noro     1660:   if ( rank >= MAXIOFP ) return -1;
                   1661:   iofp_102[rank].s = s1;
1.70      fujimoto 1662: #if defined(VISUAL) || defined(__MINGW32__)
1.76      noro     1663:   iofp_102[rank].in = WSIO_open(s1,"r");
                   1664:   iofp_102[rank].out = WSIO_open(s1,"w");
1.43      noro     1665: #else
1.76      noro     1666:   iofp_102[rank].in = fdopen(s1,"r");
                   1667:   iofp_102[rank].out = fdopen(s1,"w");
1.43      noro     1668: #if !defined(__CYGWIN__)
1.76      noro     1669:   setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
                   1670:     (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
                   1671:   setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
                   1672:     (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
                   1673: #endif
                   1674: #endif
                   1675:   if ( little_endian )
                   1676:     c = 1;
                   1677:   else
                   1678:     c = 0xff;
                   1679:   if ( is_master ) {
                   1680:     /* server : write -> read */
                   1681:     write_char((FILE *)iofp_102[rank].out,&c);
                   1682:     ox_flush_stream_force_102(rank);
                   1683:     read_char((FILE *)iofp_102[rank].in,&rc);
                   1684:   } else {
                   1685:     /* client : read -> write */
                   1686:     read_char((FILE *)iofp_102[rank].in,&rc);
                   1687:     /* special care for a failure of spawing a server */
                   1688:     if ( rc !=0 && rc != 1 && rc != 0xff )
                   1689:       return -1;
                   1690:     write_char((FILE *)iofp_102[rank].out,&c);
                   1691:     ox_flush_stream_force_102(rank);
                   1692:   }
                   1693:   iofp_102[rank].conv = c == rc ? 0 : 1;
                   1694:   iofp_102[rank].socket = 0;
                   1695:   return 0;
1.43      noro     1696: }
                   1697:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>