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

Annotation of OpenXM_contrib2/asir2018/io/tcpf.c, Revision 1.2

1.1       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
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     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.2     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2018/io/tcpf.c,v 1.1 2018/09/19 05:45:08 noro Exp $
1.1       noro       48: */
                     49: #include "ca.h"
                     50: #include "parse.h"
                     51: #include "com.h"
                     52: #include <signal.h>
                     53: #include <string.h>
                     54: #include <sys/types.h>
                     55: #include <sys/wait.h>
                     56: #include <stdlib.h>
                     57: #include <unistd.h>
                     58: #include <pwd.h>
                     59:
                     60: #include "ox.h"
                     61:
                     62: #if defined(VISUAL) || defined(__MINGW32__)
                     63: #include <winsock2.h>
                     64: #include <process.h>
                     65: #endif
                     66:
                     67: #define INIT_TAB_SIZ 64
                     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 {
                     77:   int m,c,af_unix;
                     78: } *m_c_tab;
                     79:
                     80: static int m_c_i,m_c_s;
                     81: int I_am_server;
                     82:
                     83: extern int little_endian;
                     84:
                     85: #if defined(MPI)
                     86: extern int mpi_nprocs;
                     87: #define valid_mctab_index(ind)\
                     88: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
                     89: #define check_valid_mctab_index(ind)\
                     90: if((ind)<0||(ind)>=mpi_nprocs){(ind)=-1;}
                     91: #else
                     92: #define valid_mctab_index(ind)\
                     93: if((ind)<I_am_server||(ind)>=m_c_i||\
                     94: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
                     95: #define check_valid_mctab_index(ind)\
                     96: if((ind)<I_am_server||(ind)>=m_c_i||\
                     97: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){(ind)=-1;}
                     98: #endif
                     99:
                    100: struct IOFP iofp_102[MAXIOFP];
                    101: int nserver_102;
                    102: int myrank_102;
                    103:
                    104: int register_102(int s,int rank, int is_master);
                    105:
                    106: int register_server();
                    107: int get_mcindex(int);
                    108:
                    109: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
                    110:
                    111: void Pox_launch(),Pox_launch_nox();
                    112: void Pox_launch_generic();
                    113: void Pox_shutdown();
                    114:
                    115: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
                    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();
                    130: void Pox_mpi_myid(), Pox_mpi_nprocs();
                    131: void Pox_tcp_accept_102(),Pox_tcp_connect_102();
                    132: void Pox_send_102(),Pox_recv_102();
                    133: void Pox_set_rank_102();
                    134: void Pox_get_rank_102();
                    135: void Pox_reset_102();
                    136: void Pox_bcast_102();
                    137: void Pox_reduce_102();
                    138:
                    139: void ox_launch_generic();
                    140:
                    141: pointer bevalf();
                    142:
                    143: struct ftab tcp_tab[] = {
                    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},
                    170:
                    171:   {"ox_rpc",Pox_rpc,-99999999},
                    172:   {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
                    173:
                    174:   {"ox_sync",Pox_sync,1},
                    175: #if defined(MPI)
                    176:   {"ox_mpi_myid",Pox_mpi_myid,0},
                    177:   {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
                    178: #endif
                    179: #if !defined(MPI)
                    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},
                    198:
                    199:   {"ox_setname",Pox_setname,2},
                    200:   {"ox_evalname",Pox_evalname,2},
                    201:
                    202:   {"ox_execute_string",Pox_execute_string,2},
                    203:   {"ox_execute_function",Pox_execute_function,3},
                    204:
                    205:   {0,0,0},
                    206: };
                    207:
                    208: extern struct IOFP iofp[];
                    209: extern MATHCAP my_mathcap;
                    210: extern int ox_exchange_mathcap;
                    211:
                    212: Obj asir_pop_one();
                    213: void asir_push_one(Obj);
                    214:
                    215: #if defined(MPI)
                    216: extern int mpi_myid, mpi_nprocs;
                    217:
                    218: void Pox_mpi_myid(Z *rp)
                    219: {
1.2     ! noro      220:   STOZ(mpi_myid,*rp);
1.1       noro      221: }
                    222:
                    223: void Pox_mpi_nprocs(Z *rp)
                    224: {
1.2     ! noro      225:   STOZ(mpi_nprocs,*rp);
1.1       noro      226: }
                    227: #endif
                    228:
                    229: void Pox_get_serverinfo(NODE arg,LIST *rp)
                    230: {
                    231:   int i,c;
                    232:   Z 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);
1.2     ! noro      241:         STOZ(i,s_id);
1.1       noro      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 {
1.2     ! noro      251:     i = ZTOS((Q)ARG0(arg));
1.1       noro      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:   }
                    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:
                    265: void Pgenerate_port(NODE arg,Obj *rp)
                    266: {
                    267:   char port_str[BUFSIZ];
                    268:   int port;
                    269:   char *s;
                    270:   STRING str;
                    271:   Z q;
                    272:
                    273:   if ( !arg || !ARG0(arg) ) {
                    274:     generate_port(0,port_str);
                    275:     port = atoi(port_str);
1.2     ! noro      276:     STOZ(port,q);
1.1       noro      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:   }
                    285: }
                    286:
                    287: void Pox_reset_102(NODE arg,Z *rp)
                    288: {
                    289:   int s;
1.2     ! noro      290:   int  index = ZTOS((Q)ARG0(arg));
1.1       noro      291:
                    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;
                    297: }
                    298:
                    299: void Pox_get_rank_102(LIST *rp)
                    300: {
                    301:   Z n,r;
                    302:   NODE node;
                    303:
1.2     ! noro      304:   STOZ(nserver_102,n);
        !           305:   STOZ(myrank_102,r);
1.1       noro      306:   node = mknode(2,n,r);
                    307:   MKLIST(*rp,node);
                    308: }
                    309:
                    310: void Pox_set_rank_102(NODE arg,Z *rp)
                    311: {
                    312:   Z nserver,rank;
                    313:   int s;
1.2     ! noro      314:   int  index = ZTOS((Q)ARG0(arg));
1.1       noro      315:
                    316:   valid_mctab_index(index);
                    317:   s = m_c_tab[index].c;
                    318:   nserver = (Z)ARG1(arg);
                    319:   rank = (Z)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;
                    325: }
                    326:
                    327: /* ox_tcp_accept_102(server,port,rank) */
                    328:
                    329: void Pox_tcp_accept_102(NODE arg,Z *rp)
                    330: {
                    331:   int s;
1.2     ! noro      332:   int  index = ZTOS((Q)ARG0(arg));
1.1       noro      333:
                    334:   valid_mctab_index(index);
                    335:   s = m_c_tab[index].c;
                    336:
                    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;
                    342: }
                    343:
                    344: /*
                    345:  ox_tcp_connect_102(server,host,port,rank)
                    346: */
                    347:
                    348: void Pox_tcp_connect_102(NODE arg,Z *rp)
                    349: {
                    350:   int s;
1.2     ! noro      351:   int  index = ZTOS((Q)ARG0(arg));
1.1       noro      352:
                    353:   valid_mctab_index(index);
                    354:   s = m_c_tab[index].c;
                    355:
                    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;
                    362: }
                    363:
                    364: /*
                    365:  try_bind_listen(port)
                    366: */
                    367:
                    368: void Ptry_bind_listen(NODE arg,Z *rp)
                    369: {
                    370:   char port_str[BUFSIZ];
                    371:   int port,s,use_unix;
                    372:
                    373:   if ( IS_CYGWIN || !ARG0(arg) || NUM(ARG0(arg)) ) {
1.2     ! noro      374:     port = ZTOS((Q)ARG0(arg));
1.1       noro      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);
1.2     ! noro      382:   STOZ(s,*rp);
1.1       noro      383: }
                    384:
                    385: /*
                    386:  try_connect(host,port)
                    387: */
                    388:
                    389: void Ptry_connect(NODE arg,Z *rp)
                    390: {
                    391:   char port_str[BUFSIZ];
                    392:   char *host;
                    393:   int port,s,use_unix;
                    394:
                    395:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.2     ! noro      396:     port = ZTOS((Q)ARG1(arg));
1.1       noro      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);
1.2     ! noro      405:   STOZ(s,*rp);
1.1       noro      406: }
                    407:
                    408: /*
                    409:  try_accept(sock,port)
                    410: */
                    411:
                    412: void Ptry_accept(NODE arg,Z *rp)
                    413: {
                    414:   int use_unix,s;
                    415:
                    416:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) )
                    417:     use_unix = 0;
                    418:   else
                    419:     use_unix = 1;
1.2     ! noro      420:   s = try_accept(use_unix,ZTOS((Q)ARG0(arg)));
        !           421:   STOZ(s,*rp);
1.1       noro      422: }
                    423:
                    424: /*
                    425:  register_server(cs,cport,ss,sport)
                    426: */
                    427:
                    428: void Pregister_server(NODE arg,Z *rp)
                    429: {
                    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:
1.2     ! noro      435:   cs = ZTOS((Q)ARG0(arg));
        !           436:   ss = ZTOS((Q)ARG2(arg));
1.1       noro      437:   if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.2     ! noro      438:     sprintf(cport_str,"%d",ZTOS((Q)ARG1(arg)));
1.1       noro      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");
1.2     ! noro      447:     sprintf(sport_str,"%d",ZTOS((Q)ARG3(arg)));
1.1       noro      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);
1.2     ! noro      461:     STOZ(-1,*rp);
1.1       noro      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 */
1.2     ! noro      482:   STOZ(ind,*rp);
1.1       noro      483: }
                    484:
                    485: #if !defined(VISUAL) && !defined(__MINGW32__)
                    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: {
                    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;
                    513: }
                    514:
                    515: static int find_executable_main(char *file)
                    516: {
                    517:   struct stat buf;
                    518:
                    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;
                    525: }
                    526:
                    527: #endif
                    528: /*
                    529:   ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
                    530:
                    531:   Input
                    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
                    539: */
                    540:
                    541: void Pox_launch_generic(NODE arg,Z *rp)
                    542: {
                    543:   int use_unix,use_ssh,use_x,conn_to_serv;
                    544:   char *host,*launcher,*server;
                    545:   Z 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;
1.2     ! noro      553:   conn_to_serv = ZTOS((Q)ARG6(arg));
1.1       noro      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;
                    559: }
                    560:
                    561: #if 0
                    562: void ox_launcher_101_generic(char *host,char *launcher,
                    563:     int use_unix,int use_ssh,int use_x,int conn_to_serv,Z *rp)
                    564: {
                    565:   int cs,cn,ind,id;
                    566:   char control_port_str[BUFSIZ];
                    567:   Obj obj;
                    568:
                    569: #if !defined(VISUAL) && !defined(__MINGW32__)
                    570:   if ( use_unix && !find_executable("xterm") ) use_x = 0;
                    571: #endif
                    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 );
                    589:
                    590:   /* client mode */
                    591:   cn = get_iofp(cs,control_port_str,0);
                    592:
                    593:   /* register server to the server list */
                    594:   ind = register_server_101(use_unix,cn);
                    595:
1.2     ! noro      596:   STOZ(ind,*rp);
1.1       noro      597: }
                    598: #endif
                    599:
                    600: void ox_launch_generic(char *host,char *launcher,char *server,
                    601:     int use_unix,int use_ssh,int use_x,int conn_to_serv,Z *rp)
                    602: {
                    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;
                    608:     Z 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 = (Z)BDY(NEXT(n0));
                    618:             if ( !strcmp(key,"fd") && value ) {
1.2     ! noro      619:                 fd = ZTOS(value);
1.1       noro      620:                 break;
                    621:             }
                    622:         }
                    623:     }
                    624:     if (!available_mcindex(fd)) {
1.2     ! noro      625:         STOZ(-1,*rp);
1.1       noro      626:         return;
                    627:     }
                    628: #if !defined(VISUAL) && !defined(__MINGW32__)
                    629:   if ( use_unix && !find_executable("xterm") ) use_x = 0;
                    630: #endif
                    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);
1.2     ! noro      665:     STOZ(-1,*rp);
1.1       noro      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 */
1.2     ! noro      686:   STOZ(ind,*rp);
1.1       noro      687: }
                    688:
                    689: #if defined(__CYGWIN32__)
                    690: static void bslash2slash(char *buf)
                    691: {
                    692:   char *p;
                    693:
                    694:   for ( p = buf; *p; p++ )
                    695:     if ( *p == '\\' )
                    696:       *p = '/';
                    697: }
                    698:
                    699: static int get_start_path(char *buf)
                    700: {
                    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:   }
                    749:
                    750:   return 0;
                    751: }
                    752:
                    753: static void get_launcher_path(char *buf)
                    754: {
                    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);
                    771: }
                    772: #endif
                    773:
                    774: void spawn_server(char *host,char *launcher,char *server,
                    775:   int use_unix,int use_ssh,int use_x,int conn_to_serv,
                    776:   char *control_port_str,char *server_port_str)
                    777: {
                    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];
                    784: #if !defined(VISUAL) && !defined(__MINGW32__)
                    785:   char cmd[BUFSIZ];
                    786: #endif
                    787: #if defined(__CYGWIN__)
                    788:   char win_start[BUFSIZ],win_launcher[BUFSIZ];
                    789: #endif
                    790:   void Pget_rootdir();
                    791:
                    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);
                    812: #if defined(VISUAL) || defined(__MINGW32__)
                    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);
                    831: #else
                    832:   if ( use_unix || !host ) {
                    833: #if defined(__CYGWIN32__)
                    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:     }
                    854: #else
                    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,
                    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",
                    885:         rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
                    886:         control_port_str,server_port_str,server,"1");
                    887:       else
                    888:         sprintf(cmd,
                    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",
                    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,
                    895: "%s -f -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
                    896:         rsh,host,launcher,localhost,conn_str,
                    897:         control_port_str,server_port_str,server,"1","-nolog");
                    898:       else
                    899:         sprintf(cmd,
                    900: "%s -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
                    901:         rsh,host,launcher,localhost,conn_str,
                    902:         control_port_str,server_port_str,server,dname0,"-nolog");
                    903:     system(cmd);
                    904:   }
                    905: #endif /* VISUAL */
                    906: }
                    907:
                    908: void Pox_launch(NODE arg,Obj *rp)
                    909: {
                    910:   ox_launch_main(1,arg,rp);
                    911: }
                    912:
                    913: void Pox_launch_nox(NODE arg,Obj *rp)
                    914: {
                    915:   ox_launch_main(0,arg,rp);
                    916: }
                    917:
                    918: /*
                    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
                    922: */
                    923:
                    924: void ox_launch_main(int with_x,NODE arg,Obj *p)
                    925: {
                    926:   char *str;
                    927:   char *hostname,*servername;
                    928:   char *control;
                    929:   int use_unix;
                    930:   Z 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;
                    971: }
                    972:
                    973: void extend_mctab(int bound)
                    974: {
                    975:   int i,n;
                    976:   struct m_c *t;
                    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));
                    988:         bzero((void *)t,n*sizeof(struct m_c));
                    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: {
                   1002:   if (ind < 0) return 1;
                   1003:   extend_mctab(ind);
                   1004:   return m_c_tab[ind].m<0 && m_c_tab[ind].c<0;
                   1005: }
                   1006:
                   1007: int register_server(int af_unix,int m,int c,int ind)
                   1008: {
                   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:   }
                   1023: #if !defined(MPI)
                   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++;
                   1046: }
                   1047:
                   1048: /* iofp index => m_c_tab index */
                   1049:
                   1050: int get_mcindex(int i)
                   1051: {
                   1052:   int j;
                   1053:
                   1054:   for ( j = 0; j < m_c_i; j++ )
                   1055:     if ( m_c_tab[j].c == i )
                   1056:       return j;
                   1057:   return -1;
                   1058: }
                   1059:
                   1060: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
                   1061:
                   1062: void Pox_select(NODE arg,LIST *rp)
                   1063: {
                   1064:   int fd,n,i,index,mcind,s,maxfd,minfd;
                   1065:   fd_set r,w,e;
                   1066:   NODE list,t,t1,t0;
                   1067:   Z 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) ) {
1.2     ! noro     1084:     index = ZTOS((Q)BDY(t));
1.1       noro     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:   }
                   1098:
                   1099:   n = select(FD_SETSIZE,&r,&w,&e,tvp);
                   1100: #if defined(VISUAL) || defined(__MINGW32__)
                   1101:   for ( i = minfd, t = 0; n && i <= maxfd; i++ )
                   1102: #else
                   1103:   for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
                   1104: #endif
                   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);
1.2     ! noro     1110:       n--; STOZ(mcind,q); MKNODE(t1,q,t); t = t1;
1.1       noro     1111:     }
                   1112:   MKLIST(*rp,t);
                   1113: }
                   1114:
                   1115: void Pox_flush(NODE arg,Z *rp)
                   1116: {
1.2     ! noro     1117:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1118:
                   1119:   valid_mctab_index(index);
                   1120:   ox_flush_stream_force(m_c_tab[index].c);
                   1121:   *rp = ONE;
                   1122: }
                   1123:
                   1124: void Pox_send_raw_cmo(NODE arg,Obj *rp)
                   1125: {
                   1126:   int s;
1.2     ! noro     1127:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1128:
                   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;
                   1135: }
                   1136:
                   1137: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
                   1138: {
                   1139:   int s;
1.2     ! noro     1140:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1141:
                   1142:   valid_mctab_index(index);
                   1143:   s = m_c_tab[index].c;
                   1144:   ox_read_cmo(s,rp);
                   1145: }
                   1146:
                   1147: void Pox_send_102(NODE arg,Obj *rp)
                   1148: {
1.2     ! noro     1149:   int rank = ZTOS((Q)ARG0(arg));
1.1       noro     1150:
                   1151:   ox_send_data_102(rank,(Obj)ARG1(arg));
                   1152:   *rp = 0;
                   1153: }
                   1154:
                   1155: void Pox_recv_102(NODE arg,Obj *rp)
                   1156: {
                   1157:   int id;
1.2     ! noro     1158:   int rank = ZTOS((Q)ARG0(arg));
1.1       noro     1159:
                   1160:   ox_recv_102(rank,&id,rp);
                   1161: }
                   1162:
                   1163: void Pox_bcast_102(NODE arg,Obj *rp)
                   1164: {
1.2     ! noro     1165:   int rank = ZTOS((Q)ARG0(arg));
1.1       noro     1166:   Obj data;
                   1167:
                   1168:   if ( argc(arg) > 1 )
                   1169:     asir_push_one((Obj)ARG1(arg));
                   1170:   ox_bcast_102(rank);
                   1171:   *rp = (Obj)asir_pop_one();
                   1172: }
                   1173:
                   1174: void Pox_reduce_102(NODE arg,Obj *rp)
                   1175: {
1.2     ! noro     1176:   int root = ZTOS((Q)ARG0(arg));
1.1       noro     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();
                   1195: }
                   1196:
                   1197: void Pox_push_local(NODE arg,Obj *rp)
                   1198: {
                   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.");
1.2     ! noro     1206:   index = ZTOS((Q)ARG0(arg));
1.1       noro     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;
                   1217: }
                   1218:
                   1219: void Pox_push_cmo(NODE arg,Obj *rp)
                   1220: {
                   1221:   int s;
                   1222:   int index;
                   1223:
                   1224:   if ( !arg )
                   1225:     error("ox_push_cmo : too few arguments.");
1.2     ! noro     1226:   index = ZTOS((Q)ARG0(arg));
1.1       noro     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;
                   1232: }
                   1233:
                   1234: void Pox_push_vl(NODE arg,Obj *rp)
                   1235: {
1.2     ! noro     1236:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1237:
                   1238:   valid_mctab_index(index);
                   1239:   ox_send_local_ring(m_c_tab[index].c,CO);
                   1240:   *rp = 0;
                   1241: }
                   1242:
                   1243: void Pox_pop_local(NODE arg,Obj *rp)
                   1244: {
                   1245:   int s;
1.2     ! noro     1246:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1247:
                   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);
                   1253: }
                   1254:
                   1255: void Pox_pop_cmo(NODE arg,Obj *rp)
                   1256: {
                   1257:   int s;
1.2     ! noro     1258:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1259:
                   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);
                   1265: }
                   1266:
                   1267: void Pox_pop0_local(NODE arg,Obj *rp)
                   1268: {
1.2     ! noro     1269:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1270:
                   1271:   valid_mctab_index(index);
                   1272:   ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
                   1273:   *rp = 0;
                   1274: }
                   1275:
                   1276: void Pox_pop0_cmo(NODE arg,Obj *rp)
                   1277: {
1.2     ! noro     1278:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1279:
                   1280:   valid_mctab_index(index);
                   1281:   ox_send_cmd(m_c_tab[index].c,SM_popCMO);
                   1282:   *rp = 0;
                   1283: }
                   1284:
                   1285: void Pox_pop0_string(NODE arg,STRING *rp)
                   1286: {
1.2     ! noro     1287:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1288:
                   1289:   valid_mctab_index(index);
                   1290:   ox_send_cmd(m_c_tab[index].c,SM_popString);
                   1291:   *rp = 0;
                   1292: }
                   1293:
                   1294: void Pox_pop_string(NODE arg,Obj *rp)
                   1295: {
                   1296:   int s;
1.2     ! noro     1297:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1298:
                   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);
                   1304: }
                   1305:
                   1306: void Pox_get(NODE arg,Obj *rp)
                   1307: {
                   1308:   int index;
                   1309:   int s;
                   1310:
                   1311:   if ( !arg ) {
                   1312:     /* client->server */
                   1313:     ox_get_result(0,rp);
                   1314:   } else {
                   1315:     /* server->client */
1.2     ! noro     1316:     index = ZTOS((Q)ARG0(arg));
1.1       noro     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:   }
                   1322: }
                   1323:
                   1324: void Pox_pops(NODE arg,Obj *rp)
                   1325: {
                   1326:   int s;
                   1327:   USINT n;
1.2     ! noro     1328:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1329:
                   1330:   valid_mctab_index(index);
                   1331:   s = m_c_tab[index].c;
                   1332:   if ( NEXT(arg) )
1.2     ! noro     1333:     MKUSINT(n,ZTOS((Q)ARG1(arg)));
1.1       noro     1334:   else
                   1335:     MKUSINT(n,1);
                   1336:   ox_send_data(s,n);
                   1337:   ox_send_cmd(s,SM_pops);
                   1338:   *rp = 0;
                   1339: }
                   1340:
                   1341: void Pox_execute_function(NODE arg,Obj *rp)
                   1342: {
                   1343:   int s;
                   1344:   USINT ui;
1.2     ! noro     1345:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1346:
                   1347:   valid_mctab_index(index);
                   1348:   s = m_c_tab[index].c;
1.2     ! noro     1349:   MKUSINT(ui,ZTOS((Q)ARG2(arg)));
1.1       noro     1350:   ox_send_data(s,ui);
                   1351:   ox_send_data(s,ARG1(arg));
                   1352:   ox_send_cmd(s,SM_executeFunction);
                   1353:   *rp = 0;
                   1354: }
                   1355:
                   1356: void Pox_setname(NODE arg,Obj *rp)
                   1357: {
                   1358:   int s;
1.2     ! noro     1359:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1360:
                   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;
                   1366: }
                   1367:
                   1368: void Pox_evalname(NODE arg,Obj *rp)
                   1369: {
                   1370:   int s;
1.2     ! noro     1371:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1372:
                   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;
                   1378: }
                   1379:
                   1380: void Pox_execute_string(NODE arg,Obj *rp)
                   1381: {
                   1382:   int s;
1.2     ! noro     1383:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1384:
                   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;
                   1391: }
                   1392:
                   1393: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
                   1394:
                   1395: void Pox_rpc(NODE arg,Obj *rp)
                   1396: {
                   1397:   int s,i,n;
                   1398:   STRING f;
                   1399:   USINT ui;
                   1400:   pointer *w;
                   1401:   NODE t;
1.2     ! noro     1402:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     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;
                   1419: }
                   1420:
                   1421: void Pox_cmo_rpc(NODE arg,Obj *rp)
                   1422: {
                   1423:   int s,i,n;
                   1424:   STRING f;
                   1425:   USINT ui;
                   1426:   NODE t;
                   1427:   Obj dmy;
                   1428:   pointer *w;
1.2     ! noro     1429:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1430:   int find;
                   1431:   Obj sync;
                   1432:
                   1433:   find = get_opt("sync",&sync);
                   1434:   valid_mctab_index(index);
                   1435:   s = m_c_tab[index].c; arg = NEXT(arg);
                   1436:   f = (STRING)BDY(arg); arg = NEXT(arg);
                   1437:   for ( n = 0, t = arg; t; t = NEXT(t), n++ );
                   1438:   w = (pointer *)ALLOCA(n*sizeof(pointer));
                   1439:   for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
                   1440:     w[i] = BDY(t);
                   1441:   for ( i = n-1; i >= 0; i-- )
                   1442:     ox_send_data(s,w[i]);
                   1443:   MKUSINT(ui,n);
                   1444:   ox_send_data(s,ui);
                   1445:   ox_send_data(s,f);
                   1446:   if ( find && sync ) {
                   1447:     ox_send_cmd(s,SM_executeFunctionSync);
                   1448:     ox_get_result(s,&dmy);
                   1449:   } else
                   1450:     ox_send_cmd(s,SM_executeFunction);
                   1451:   *rp = 0;
                   1452: }
                   1453:
                   1454: int No_ox_reset;
                   1455: extern Z ox_pari_stream;
                   1456: extern int ox_pari_stream_initialized;
                   1457:
                   1458: void Pox_reset(NODE arg,Z *rp)
                   1459: {
                   1460:   USINT t;
                   1461:   int id,c,m;
                   1462:   Obj obj;
                   1463:   NODE nd;
                   1464:   Z q;
1.2     ! noro     1465:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1466:
                   1467:   valid_mctab_index(index);
                   1468:   m = m_c_tab[index].m;
                   1469:   c = m_c_tab[index].c;
                   1470:   if ( m >= 0 ) {
                   1471:     if ( no_ox_reset(c) == 1 ) {
1.2     ! noro     1472:       STOZ(index,q);
1.1       noro     1473:       nd = mknode(1,q);
                   1474:       switch ( No_ox_reset ) {
                   1475:       case 1:
                   1476:          fprintf(stderr,"The server does not implenent OX reset protocol.\n");
                   1477:          fprintf(stderr,"The server is terminated.\n");
                   1478:          Pox_shutdown(nd,rp);
1.2     ! noro     1479:          if ( index == ZTOS(ox_pari_stream) ) ox_pari_stream_initialized = 0;
1.1       noro     1480:          break;
                   1481:       case 2:
                   1482:          Pox_shutdown(nd,rp);
1.2     ! noro     1483:          if ( index == ZTOS(ox_pari_stream) ) ox_pari_stream_initialized = 0;
1.1       noro     1484:          break;
                   1485:       default:
                   1486:          error("The server does not implement OX reset protocol.");
                   1487:          *rp = ONE;
                   1488:          break;
                   1489:       }
                   1490:       return;
                   1491:     }
                   1492:
                   1493:     if ( argc(arg) == 1 ) {
                   1494:       ox_send_cmd(m,SM_control_reset_connection);
                   1495:       ox_flush_stream_force(m);
                   1496: #if 0
                   1497:       /* XXX obsolete */
                   1498:       ox_recv(m,&id,&obj); t = (USINT)obj;
                   1499: #endif
                   1500:     }
                   1501:     *rp = ONE;
                   1502: #if defined(VISUAL) || defined(__MINGW32__)
                   1503:     Sleep(100);
                   1504:     ox_send_cmd(c,SM_nop);
                   1505:     ox_flush_stream_force(c);
                   1506: #endif
                   1507:     while ( 1 ) {
                   1508:       ox_recv(c,&id,&obj);
                   1509:       if ( id == OX_SYNC_BALL )
                   1510:         break;
                   1511:     }
                   1512:     ox_send_sync(c);
                   1513:   } else
                   1514:     *rp = 0;
                   1515: }
                   1516:
                   1517: void Pox_intr(NODE arg,Z *rp)
                   1518: {
                   1519:   int m;
1.2     ! noro     1520:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1521:
                   1522:   valid_mctab_index(index);
                   1523:   m = m_c_tab[index].m;
                   1524:   if ( m >= 0 ) {
                   1525:     if ( argc(arg) == 1 ) {
                   1526:       ox_send_cmd(m,SM_control_intr);
                   1527:       ox_flush_stream_force(m);
                   1528:     }
                   1529:     *rp = ONE;
                   1530:   } else
                   1531:     *rp = 0;
                   1532: }
                   1533:
                   1534: void Pox_sync(NODE arg,Z *rp)
                   1535: {
                   1536:   int c;
1.2     ! noro     1537:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1538:
                   1539:   valid_mctab_index(index);
                   1540:   c = m_c_tab[index].c;
                   1541:   ox_send_sync(c);
                   1542:   *rp = 0;
                   1543: }
                   1544:
                   1545: void Pox_shutdown(NODE arg,Z *rp)
                   1546: {
                   1547:   int s;
1.2     ! noro     1548:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1549: #if !defined(VISUAL) && !defined(__MINGW32__)
                   1550:   int status;
                   1551: #endif
                   1552:
                   1553:   valid_mctab_index(index);
                   1554:   s = m_c_tab[index].m;
                   1555:   ox_send_cmd(s,SM_shutdown);
                   1556:   free_iofp(s);
                   1557:   s = m_c_tab[index].c;
                   1558:   free_iofp(s);
                   1559: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__)
                   1560:   if ( m_c_tab[index].af_unix )
                   1561:     wait(&status);
                   1562: #endif
                   1563:   m_c_tab[index].m = -1; m_c_tab[index].c = -1;
                   1564:   m_c_tab[index].af_unix = 0;
                   1565:   *rp = 0;
                   1566: }
                   1567:
                   1568: void Pox_push_cmd(NODE arg,Z *rp)
                   1569: {
                   1570:   int ui;
1.2     ! noro     1571:   int index = ZTOS((Q)ARG0(arg));
1.1       noro     1572:
                   1573:   valid_mctab_index(index);
1.2     ! noro     1574:   ui = ZTOS((Q)ARG1(arg));
1.1       noro     1575:   ox_send_cmd(m_c_tab[index].c,ui);
                   1576:   *rp = 0;
                   1577: }
                   1578:
                   1579: void shutdown_all() {
                   1580:   int s;
                   1581:   int i,index;
                   1582: #if !defined(VISUAL) && !defined(__MINGW32__)
                   1583:   int status;
                   1584: #endif
                   1585:
                   1586:   for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
                   1587:     index = i;
                   1588:     check_valid_mctab_index(index);
                   1589:     if ( index < 0 )
                   1590:       continue;
                   1591:     s = m_c_tab[index].m;
                   1592:     ox_send_cmd(s,SM_shutdown);
                   1593: #if defined(VISUAL) || defined(__MINGW32__)
                   1594:   Sleep(1000);
                   1595: #endif
                   1596:     free_iofp(s);
                   1597:     s = m_c_tab[index].c;
                   1598:     free_iofp(s);
                   1599: #if !defined(MPI) && !defined(VISUAL) && !defined(__MINGW32__)
                   1600:     if ( m_c_tab[index].af_unix )
                   1601:       wait(&status);
                   1602: #endif
                   1603:     m_c_tab[index].m = 0; m_c_tab[index].c = 0;
                   1604:     m_c_tab[index].af_unix = 0;
                   1605:   }
                   1606: }
                   1607:
                   1608: char *ox_get_servername(int);
                   1609:
                   1610: int is_ox_plot(int index)
                   1611: {
                   1612:   char *name;
                   1613:
                   1614:   check_valid_mctab_index(index);
                   1615:   if ( index < 0 )
                   1616:     return 0;
                   1617:   /* m : client, c : server ??? */
                   1618:   name = ox_get_servername(m_c_tab[index].c);
                   1619:   return strcmp(name,"ox_plot") ? 0 : 1;
                   1620: }
                   1621:
                   1622: int debug_plot;
                   1623:
                   1624: int validate_ox_plot_stream(int index)
                   1625: {
                   1626:   int i;
                   1627:   NODE arg;
                   1628:   STRING name;
                   1629:   Obj r;
                   1630:
                   1631:   if ( is_ox_plot(index) )
                   1632:     return index;
                   1633:   for ( i = 0; i < m_c_i; i++ )
                   1634:     if ( is_ox_plot(i) )
                   1635:       return i;
                   1636:
                   1637:   /* create an ox_plot server */
                   1638:   MKSTR(name,"ox_plot");
                   1639:   arg = mknode(2,NULL,name);
                   1640:   if ( debug_plot ) Pox_launch(arg,&r);
                   1641:   else Pox_launch_nox(arg,&r);
1.2     ! noro     1642:   i = ZTOS((Q)r);
1.1       noro     1643: #if defined(VISUAL) || defined(__MINGW32__)
                   1644:   Sleep(100);
                   1645:   ox_send_cmd(m_c_tab[i].c,SM_nop);
                   1646:   ox_flush_stream_force(m_c_tab[i].c);
                   1647: #endif
                   1648:   return i;
                   1649: }
                   1650:
                   1651: int get_ox_server_id(int index)
                   1652: {
                   1653:   valid_mctab_index(index);
                   1654:   return m_c_tab[index].c;
                   1655: }
                   1656:
                   1657: int register_102(int s1,int rank,int is_master)
                   1658: {
                   1659:   unsigned char c,rc;
                   1660:
                   1661:   if ( rank >= MAXIOFP ) return -1;
                   1662:   iofp_102[rank].s = s1;
                   1663: #if defined(VISUAL) || defined(__MINGW32__)
                   1664:   iofp_102[rank].in = WSIO_open(s1,"r");
                   1665:   iofp_102[rank].out = WSIO_open(s1,"w");
                   1666: #else
                   1667:   iofp_102[rank].in = fdopen(s1,"r");
                   1668:   iofp_102[rank].out = fdopen(s1,"w");
                   1669: #if !defined(__CYGWIN__)
                   1670:   setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
                   1671:     (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
                   1672:   setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
                   1673:     (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
                   1674: #endif
                   1675: #endif
                   1676:   if ( little_endian )
                   1677:     c = 1;
                   1678:   else
                   1679:     c = 0xff;
                   1680:   if ( is_master ) {
                   1681:     /* server : write -> read */
                   1682:     write_char((FILE *)iofp_102[rank].out,&c);
                   1683:     ox_flush_stream_force_102(rank);
                   1684:     read_char((FILE *)iofp_102[rank].in,&rc);
                   1685:   } else {
                   1686:     /* client : read -> write */
                   1687:     read_char((FILE *)iofp_102[rank].in,&rc);
                   1688:     /* special care for a failure of spawing a server */
                   1689:     if ( rc !=0 && rc != 1 && rc != 0xff )
                   1690:       return -1;
                   1691:     write_char((FILE *)iofp_102[rank].out,&c);
                   1692:     ox_flush_stream_force_102(rank);
                   1693:   }
                   1694:   iofp_102[rank].conv = c == rc ? 0 : 1;
                   1695:   iofp_102[rank].socket = 0;
                   1696:   return 0;
                   1697: }
                   1698:

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