[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.58

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

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