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

Annotation of OpenXM_contrib2/asir2000/io/ox_asir.c, Revision 1.26

1.15      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.16      noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.15      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.26    ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.25 2001/08/20 09:03:26 noro Exp $
1.15      noro       48: */
1.1       noro       49: #include "ca.h"
                     50: #include "parse.h"
1.6       noro       51: #include "signal.h"
1.1       noro       52: #include "ox.h"
                     53: #include "version.h"
1.6       noro       54: #if PARI
                     55: #include "genpari.h"
                     56: #endif
1.1       noro       57:
                     58: void ox_usr1_handler();
1.13      noro       59: int asir_ox_init();
1.1       noro       60:
                     61: extern jmp_buf environnement;
                     62:
                     63: extern int do_message;
                     64: extern int ox_flushing;
                     65: extern jmp_buf ox_env;
                     66: extern MATHCAP my_mathcap;
                     67:
1.11      noro       68: extern int little_endian,ox_sock_id;
                     69:
1.1       noro       70: int ox_sock_id;
1.11      noro       71: int lib_ox_need_conv;
1.1       noro       72:
1.19      noro       73: void create_error(ERR *,unsigned int ,char *);
                     74:
1.20      noro       75: int asir_OperandStackSize;
                     76: Obj *asir_OperandStack;
                     77: int asir_OperandStackPtr = -1;
                     78:
                     79: void ox_io_init();
                     80: void ox_asir_init(int,char **);
                     81: Obj asir_pop_one();
                     82: Obj asir_peek_one();
                     83: void asir_push_one(Obj);
                     84: void asir_end_flush();
                     85: int asir_executeString();
                     86: void asir_evalName(unsigned int);
                     87: void asir_setName(unsigned int);
                     88: void asir_pops();
                     89: void asir_popString();
                     90: void asir_popCMO(unsigned int);
                     91: void asir_popSerializedLocalObject();
                     92: void asir_pushCMOtag(unsigned int);
                     93: LIST asir_GetErrorList();
                     94: char *name_of_cmd(int);
                     95: char *name_of_id(int);
                     96:
                     97: static void asir_do_cmd(int,unsigned int);
1.1       noro       98: static void asir_executeFunction(int);
                     99:
                    100: #if MPI
1.14      noro      101: /* XXX : currently MPI version supports only a homogeneous cluster. */
                    102:
1.1       noro      103: extern int mpi_nprocs,mpi_myid;
                    104:
                    105: void ox_mpi_master_init() {
1.14      noro      106:        int i,idx;
1.1       noro      107:
1.14      noro      108:        for ( i = 0; i < mpi_nprocs; i++ ) {
                    109:                /* ordering information is not exchanged */
                    110:                /* idx should be equal to i */
1.1       noro      111:                idx = get_iofp(i,0,0);
1.14      noro      112:                register_server(0,idx,idx);
1.1       noro      113:        }
                    114: }
                    115:
                    116: void ox_mpi_slave_init() {
1.14      noro      117:        int i,idx;
                    118:
1.1       noro      119:        endian_init();
                    120:        fclose(stdin);
1.14      noro      121:        for ( i = 0; i < mpi_nprocs; i++ ) {
                    122:                /* ordering information is not exchanged */
                    123:                /* idx should be equal to i */
                    124:                idx = get_iofp(i,0,0);
                    125:                register_server(0,idx,idx);
                    126:        }
1.1       noro      127:        asir_OperandStackSize = BUFSIZ;
                    128:        asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
                    129:        asir_OperandStackPtr = -1;
                    130: }
                    131: #endif
                    132:
                    133: void ox_main(int argc,char **argv) {
                    134:        int id;
1.13      noro      135:        int cmd;
1.1       noro      136:        Obj obj;
                    137:        USINT ui;
                    138:        ERR err;
                    139:        LIST list;
                    140:        NODE n,n1;
                    141:        unsigned int serial;
                    142:        int ret;
                    143:        extern char LastError[];
                    144:
                    145:        ox_asir_init(argc,argv);
                    146:        if ( do_message )
                    147:                fprintf(stderr,"I'm an ox_asir, Version %d.\n",ASIR_VERSION);
                    148:        if ( setjmp(ox_env) ) {
                    149:                while ( NEXT(asir_infile) )
                    150:                        closecurrentinput();
                    151:                ox_send_sync(0);
                    152:        }
                    153:        while ( 1 ) {
                    154:                extern int recv_intr;
                    155:
                    156:                serial = ox_recv(0,&id,&obj);
                    157: #if defined(VISUAL)
                    158:                if ( recv_intr ) {
                    159:                        if ( recv_intr == 1 ) {
                    160:                                recv_intr = 0;
                    161:                                int_handler(SIGINT);
                    162:                        } else {
                    163:                                recv_intr = 0;
                    164:                                ox_usr1_handler(0);
                    165:                        }
                    166:                }
                    167: #endif
                    168:                if ( do_message )
                    169:                        fprintf(stderr,"#%d Got %s",serial,name_of_id(id));
                    170:                switch ( id ) {
                    171:                        case OX_COMMAND:
                    172:                                cmd = ((USINT)obj)->body;
                    173:                                if ( ox_flushing )
                    174:                                        break;
                    175:                                if ( do_message )
                    176:                                        fprintf(stderr," %s\n",name_of_cmd(cmd));
1.25      noro      177:                                if ( ret = setjmp(main_env) ) {
1.1       noro      178:                                        if ( ret == 1 ) {
                    179:                                                create_error(&err,serial,LastError);
                    180:                                                asir_push_one((Obj)err);
                    181:                                        }
                    182:                                        break;
                    183:                                }
                    184:                                asir_do_cmd(cmd,serial);
                    185:                                break;
                    186:                        case OX_DATA:
                    187:                        case OX_LOCAL_OBJECT_ASIR:
                    188:                                if ( ox_flushing )
                    189:                                        break;
                    190:                                if ( do_message )
                    191:                                        fprintf(stderr," -> data pushed");
                    192:                                asir_push_one(obj);
                    193:                                break;
                    194:                        case OX_SYNC_BALL:
                    195:                                asir_end_flush();
                    196:                                break;
                    197:                        default:
                    198:                                break;
                    199:                }
                    200:                if ( do_message )
                    201:                        fprintf(stderr,"\n");
                    202:        }
                    203: }
                    204:
1.13      noro      205: static void asir_do_cmd(int cmd,unsigned int serial)
1.1       noro      206: {
                    207:        MATHCAP client_mathcap;
                    208:        Q q;
                    209:        int i;
                    210:        LIST list;
                    211:
                    212:        switch ( cmd ) {
                    213:                case SM_dupErrors:
                    214:                        list = asir_GetErrorList();
                    215:                        asir_push_one((Obj)list);
                    216:                        break;
                    217:                case SM_getsp:
                    218:                        i = asir_OperandStackPtr+1;
                    219:                        STOQ(i,q);
                    220:                        asir_push_one((Obj)q);
                    221:                        break;
                    222:                case SM_popSerializedLocalObject:
                    223:                        asir_popSerializedLocalObject();
                    224:                        break;
                    225:                case SM_popCMO:
                    226:                        asir_popCMO(serial);
                    227:                        break;
                    228:                case SM_popString:
                    229:                        asir_popString();
                    230:                        break;
                    231:                case SM_setName:
                    232:                        asir_setName(serial);
                    233:                        break;
                    234:                case SM_evalName:
                    235:                        asir_evalName(serial);
                    236:                        break;
                    237:                case SM_executeStringByLocalParser:
                    238:                        asir_executeString();
                    239:                        break;
                    240:                case SM_executeStringByLocalParserInBatchMode:
                    241:                        asir_executeString();
                    242:                        asir_pop_one();
                    243:                        break;
                    244:                case SM_executeFunction:
                    245:                        asir_executeFunction(serial);
                    246:                        break;
                    247:                case SM_shutdown:
                    248:                        asir_terminate(2);
                    249:                        break;
                    250:                case SM_pops:
                    251:                        asir_pops();
                    252:                        break;
                    253:                case SM_mathcap:
                    254:                        asir_push_one((Obj)my_mathcap);
                    255:                        break;
                    256:                case SM_setMathcap:
                    257:                        client_mathcap = (MATHCAP)asir_pop_one();
                    258:                        store_remote_mathcap(0,client_mathcap);
                    259:                        break;
1.18      noro      260:                case SM_pushCMOtag:
                    261:                        asir_pushCMOtag(serial);
                    262:                        break;
1.1       noro      263:                case SM_nop:
                    264:                default:
                    265:                        break;
                    266:        }
                    267: }
                    268:
1.20      noro      269: char *name_of_id(int id)
1.1       noro      270: {
                    271:        switch ( id ) {
                    272:                case OX_COMMAND:
                    273:                        return "OX_COMMAND";
                    274:                        break;
                    275:                case OX_DATA:
                    276:                        return "OX_DATA";
                    277:                        break;
                    278:                case OX_LOCAL_OBJECT_ASIR:
                    279:                        return "OX_LOCAL_OBJECT_ASIR";
                    280:                        break;
                    281:                case OX_SYNC_BALL:
                    282:                        return "OX_SYNC_BALL";
                    283:                        break;
                    284:                default:
                    285:                        return "Unknown id";
                    286:                        break;
                    287:        }
                    288: }
                    289:
1.20      noro      290: char *name_of_cmd(int cmd)
1.1       noro      291: {
                    292:        switch ( cmd ) {
                    293:                case SM_popSerializedLocalObject:
                    294:                        return "SM_popSerializedLocalObject";
                    295:                        break;
                    296:                case SM_popCMO:
                    297:                        return "SM_popCMO";
                    298:                        break;
                    299:                case SM_popString:
                    300:                        return "SM_popString";
                    301:                        break;
                    302:                case SM_pops:
                    303:                        return "SM_pops";
                    304:                        break;
                    305:                case SM_setName:
                    306:                        return "SM_setName";
                    307:                        break;
                    308:                case SM_evalName:
                    309:                        return "SM_evalName";
                    310:                        break;
                    311:                case SM_executeStringByLocalParser:
                    312:                        return "SM_executeString";
                    313:                        break;
                    314:                case SM_executeFunction:
                    315:                        return "SM_executeFunction";
                    316:                        break;
                    317:                case SM_shutdown:
                    318:                        return "SM_shutdown";
                    319:                        break;
                    320:                case SM_beginBlock:
                    321:                        return "SM_beginBlock";
                    322:                        break;
                    323:                case SM_endBlock:
                    324:                        return "SM_endBlock";
                    325:                        break;
                    326:                case SM_mathcap:
                    327:                        return "SM_mathcap";
                    328:                        break;
                    329:                case SM_setMathcap:
                    330:                        return "SM_setMathcap";
                    331:                        break;
                    332:                case SM_getsp:
                    333:                        return "SM_setMathcap";
                    334:                        break;
                    335:                case SM_dupErrors:
                    336:                        return "SM_dupErrors";
                    337:                        break;
                    338:                case SM_nop:
                    339:                        return "SM_nop";
1.18      noro      340:                case SM_pushCMOtag:
                    341:                        return "SM_pushCMOtag";
1.1       noro      342:                default:
                    343:                        return "Unknown cmd";
                    344:                        break;
                    345:        }
                    346: }
                    347:
1.20      noro      348: LIST asir_GetErrorList()
1.1       noro      349: {
                    350:        int i;
                    351:        NODE n,n0;
                    352:        LIST err;
                    353:        Obj obj;
                    354:
                    355:        for ( i = 0, n0 = 0; i <= asir_OperandStackPtr; i++ )
                    356:                if ( (obj = asir_OperandStack[i]) && (OID(obj) == O_ERR) ) {
                    357:                        NEXTNODE(n0,n); BDY(n) = (pointer)obj;
                    358:                }
                    359:        if ( n0 )
                    360:                NEXT(n) = 0;
                    361:        MKLIST(err,n0);
                    362:        return err;
                    363: }
                    364:
1.20      noro      365: void asir_popSerializedLocalObject()
1.1       noro      366: {
                    367:        Obj obj;
                    368:        VL t,vl;
                    369:
                    370:        obj = asir_pop_one();
                    371:        get_vars_recursive(obj,&vl);
                    372:        for ( t = vl; t; t = NEXT(t) )
                    373:                if ( t->v->attr == (pointer)V_UC )
                    374:                        error("bsave : not implemented");
                    375:        ox_send_cmd(0,SM_beginBlock);
                    376:        ox_send_local_ring(0,vl);
                    377:        ox_send_local_data(0,obj);
                    378:        ox_send_cmd(0,SM_endBlock);
                    379: }
                    380:
1.20      noro      381: void asir_popCMO(unsigned int serial)
1.1       noro      382: {
                    383:        Obj obj;
                    384:        ERR err;
                    385:
                    386:        obj = asir_pop_one();
                    387:        if ( valid_as_cmo(obj) )
                    388:                ox_send_data(0,obj);
                    389:        else {
                    390:                create_error(&err,serial,"cannot convert to CMO object");
                    391:                ox_send_data(0,err);
                    392:                asir_push_one(obj);
                    393:        }
                    394: }
                    395:
1.20      noro      396: void asir_pushCMOtag(unsigned int serial)
1.18      noro      397: {
                    398:        Obj obj;
                    399:        ERR err;
                    400:        USINT ui;
                    401:        int tag;
                    402:
                    403:        obj = asir_peek_one();
                    404:        if ( cmo_tag(obj,&tag) ) {
                    405:                MKUSINT(ui,tag);
                    406:                asir_push_one((Obj)ui);
                    407:        } else {
                    408:                create_error(&err,serial,"cannot convert to CMO object");
                    409:                asir_push_one((Obj)err);
                    410:        }
                    411: }
                    412:
1.20      noro      413: void asir_popString()
1.1       noro      414: {
                    415:        Obj val;
                    416:        char *buf,*obuf;
                    417:        int l;
                    418:        STRING str;
                    419:
                    420:        val = asir_pop_one();
                    421:        if ( !val )
1.24      noro      422:                obuf = "0";
1.1       noro      423:        else {
                    424:                l = estimate_length(CO,val);
                    425:                buf = (char *)ALLOCA(l+1);
                    426:                soutput_init(buf);
                    427:                sprintexpr(CO,val);
                    428:                l = strlen(buf);
                    429:                obuf = (char *)MALLOC(l+1);
                    430:                strcpy(obuf,buf);
                    431:        }
                    432:        MKSTR(str,obuf);
                    433:        ox_send_data(0,str);
                    434: }
                    435:
1.20      noro      436: void asir_pops()
1.1       noro      437: {
                    438:        int n;
                    439:
                    440:        n = (int)(((USINT)asir_pop_one())->body);
                    441:        asir_OperandStackPtr = MAX(asir_OperandStackPtr-n,-1);
                    442: }
                    443:
1.20      noro      444: void asir_setName(unsigned int serial)
1.1       noro      445: {
                    446:        char *name;
                    447:        int l,n;
                    448:        char *dummy = "=0;";
                    449:        SNODE snode;
                    450:        ERR err;
                    451:
                    452:        name = ((STRING)asir_pop_one())->body;
                    453:        l = strlen(name);
                    454:        n = l+strlen(dummy)+1;
                    455:        parse_strp = (char *)ALLOCA(n);
                    456:        sprintf(parse_strp,"%s%s",name,dummy);
                    457:        if ( mainparse(&snode) ) {
                    458:                create_error(&err,serial,"cannot set to variable");
                    459:                asir_push_one((Obj)err);
                    460:        } else {
                    461:                FA1((FNODE)FA0(snode)) = (pointer)mkfnode(1,I_FORMULA,asir_pop_one());
                    462:                evalstat(snode);
                    463:        }
                    464: }
                    465:
1.20      noro      466: void asir_evalName(unsigned int serial)
1.1       noro      467: {
                    468:        char *name;
                    469:        int l,n;
                    470:        SNODE snode;
                    471:        ERR err;
                    472:        pointer val;
                    473:
                    474:        name = ((STRING)asir_pop_one())->body;
                    475:        l = strlen(name);
                    476:        n = l+2;
                    477:        parse_strp = (char *)ALLOCA(n);
                    478:        sprintf(parse_strp,"%s;",name);
                    479:        if ( mainparse(&snode) ) {
                    480:                create_error(&err,serial,"no such variable");
                    481:                val = (pointer)err;
                    482:        } else
                    483:                val = evalstat(snode);
                    484:        asir_push_one(val);
                    485: }
                    486:
1.20      noro      487: int asir_executeString()
1.1       noro      488: {
                    489:        SNODE snode;
                    490:        pointer val;
                    491:        char *cmd;
                    492: #if PARI
                    493:        recover(0);
                    494:        if ( setjmp(environnement) ) {
                    495:                avma = top; recover(1);
                    496:                resetenv("");
                    497:        }
                    498: #endif
                    499:        cmd = ((STRING)asir_pop_one())->body;
                    500:        parse_strp = cmd;
                    501:        if ( mainparse(&snode) ) {
                    502:                return -1;
                    503:        }
                    504:        val = evalstat(snode);
                    505:        if ( NEXT(asir_infile) ) {
                    506:                while ( NEXT(asir_infile) ) {
                    507:                        if ( mainparse(&snode) ) {
                    508:                                asir_push_one(val);
                    509:                                return -1;
                    510:                        }
                    511:                        nextbp = 0;
                    512:                        val = evalstat(snode);
                    513:                }
                    514:        }
                    515:        asir_push_one(val);
                    516:        return 0;
                    517: }
                    518:
                    519: static void asir_executeFunction(int serial)
                    520: {
                    521:        char *func;
                    522:        int argc;
                    523:        FUNC f;
                    524:        Obj result;
                    525:        VL vl;
                    526:        NODE n,n1;
                    527:        STRING fname;
                    528:        char *path;
                    529:        USINT ui;
                    530:        ERR err;
1.5       noro      531:        Obj arg;
1.1       noro      532:        static char buf[BUFSIZ];
                    533:
1.5       noro      534:        arg = asir_pop_one();
                    535:        if ( !arg || OID(arg) != O_STR ) {
                    536:                sprintf(buf,"executeFunction : invalid function name");
                    537:                goto error;
                    538:        } else
                    539:                func = ((STRING)arg)->body;
                    540:
                    541:        arg = asir_pop_one();
                    542:        if ( !arg || OID(arg) != O_USINT ) {
                    543:                sprintf(buf,"executeFunction : invalid argc");
                    544:                goto error;
                    545:        } else
                    546:                argc = (int)(((USINT)arg)->body);
1.1       noro      547:
                    548:        for ( n = 0; argc; argc-- ) {
                    549:                NEXTNODE(n,n1);
                    550:                BDY(n1) = (pointer)asir_pop_one();
                    551:        }
                    552:        if ( n )
                    553:                NEXT(n1) = 0;
                    554:
                    555:        if ( !strcmp(func,"load") ) {
                    556:                fname = (STRING)BDY(n);
                    557:                if ( OID(fname) == O_STR ) {
                    558:                        searchasirpath(BDY(fname),&path);
                    559:                        if ( path ) {
                    560:                                if ( do_message )
                    561:                                        fprintf(stderr,"loading %s\n",path);
                    562:                                execasirfile(path);
                    563:                        } else
                    564:                                if ( do_message )
                    565:                                        fprintf(stderr,"load : %s not found in the search path\n",BDY(fname));
                    566:                }
                    567:                result = 0;
                    568:        } else {
                    569:                searchf(noargsysf,func,&f);
                    570:                if ( !f )
                    571:                        searchf(sysf,func,&f);
                    572:                if ( !f )
                    573:                        searchf(ubinf,func,&f);
                    574:                if ( !f )
                    575:                        searchf(usrf,func,&f);
                    576:                if ( !f ) {
                    577:                        sprintf(buf,"executeFunction : the function %s not found",func);
1.5       noro      578:                        goto error;
1.1       noro      579:                } else {
                    580:                        result = (Obj)bevalf(f,n);
                    581:                }
                    582:        }
1.5       noro      583:        asir_push_one(result);
                    584:        return;
                    585:
                    586: error:
                    587:        create_error(&err,serial,buf);
                    588:        result = (Obj)err;
1.1       noro      589:        asir_push_one(result);
                    590: }
                    591:
1.20      noro      592: void asir_end_flush()
1.1       noro      593: {
                    594:        ox_flushing = 0;
                    595: }
                    596:
                    597: /*
                    598:   asir_OperandStackPtr points to the surface of the stack.
                    599:   That is, the data at the stack top is
                    600:        asir_OperandStack[asir_OperandStackPtr].
                    601: */
                    602:
                    603:
1.20      noro      604: void asir_push_one(Obj obj)
1.1       noro      605: {
                    606:        if ( !obj || OID(obj) != O_VOID ) {
                    607:                asir_OperandStackPtr++;
                    608:                if ( asir_OperandStackPtr >= asir_OperandStackSize ) {
                    609:                        asir_OperandStackSize += BUFSIZ;
                    610:                        asir_OperandStack
                    611:                                = (Obj *)REALLOC(asir_OperandStack,
                    612:                                        asir_OperandStackSize*sizeof(Obj));
                    613:                }
                    614:                asir_OperandStack[asir_OperandStackPtr] = obj;
                    615:        }
                    616: }
                    617:
1.20      noro      618: Obj asir_pop_one() {
1.1       noro      619:        if ( asir_OperandStackPtr < 0 ) {
                    620:                if ( do_message )
                    621:                        fprintf(stderr,"OperandStack underflow");
                    622:                return 0;
                    623:        } else {
                    624:                if ( do_message )
                    625:                        fprintf(stderr,"pop at %d\n",asir_OperandStackPtr);
                    626:                return asir_OperandStack[asir_OperandStackPtr--];
1.18      noro      627:        }
                    628: }
                    629:
1.20      noro      630: Obj asir_peek_one() {
1.18      noro      631:        if ( asir_OperandStackPtr < 0 ) {
                    632:                if ( do_message )
                    633:                        fprintf(stderr,"OperandStack underflow");
                    634:                return 0;
                    635:        } else {
                    636:                if ( do_message )
                    637:                        fprintf(stderr,"peek at %d\n",asir_OperandStackPtr);
                    638:                return asir_OperandStack[asir_OperandStackPtr];
1.1       noro      639:        }
                    640: }
                    641:
1.20      noro      642: void ox_asir_init(int argc,char **argv)
1.1       noro      643: {
                    644:        int tmp;
                    645:        char ifname[BUFSIZ];
                    646:        extern int GC_dont_gc;
                    647:        extern int read_exec_file;
                    648:        extern int do_asirrc;
                    649:        extern int do_server_in_X11;
                    650:        char *getenv();
                    651:        static ox_asir_initialized = 0;
                    652:        FILE *ifp;
1.4       noro      653:        char *homedir;
                    654:        char *ptr;
1.1       noro      655:
                    656: #if !defined(VISUAL) && !MPI
                    657:        do_server_in_X11 = 1; /* XXX */
                    658: #endif
                    659:        asir_save_handler();
                    660: #if PARI
                    661:        risa_pari_init();
                    662: #endif
                    663:        srandom((int)get_current_time());
                    664:
                    665:        rtime_init();
                    666:        env_init();
                    667:        endian_init();
                    668:        GC_init();
                    669:        process_args(--argc,++argv);
                    670:        output_init();
                    671:        arf_init();
                    672:        nglob_init();
                    673:        glob_init();
                    674:        sig_init();
                    675:        tty_init();
                    676:        debug_init();
                    677:        pf_init();
                    678:        sysf_init();
                    679:        parif_init();
                    680: #if defined(VISUAL)
                    681:        init_socket();
                    682: #endif
                    683: #if defined(UINIT)
                    684:        reg_sysf();
                    685: #endif
1.4       noro      686: /* if ASIR_CONFIG is set, execute it; else execute .asirrc */
                    687:        if ( ptr = getenv("ASIR_CONFIG") )
                    688:                strcpy(ifname,ptr);
                    689:        else {
                    690:                homedir = getenv("HOME");
                    691:                if ( !homedir ) {
                    692:                        char rootname[BUFSIZ];
                    693:
                    694:                        get_rootdir(rootname,sizeof(rootname));
                    695:                        homedir = rootname;
                    696:                }
                    697:                sprintf(ifname,"%s/.asirrc",homedir);
                    698:        }
1.1       noro      699:        if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
                    700:                input_init(ifp,ifname);
1.25      noro      701:                if ( !setjmp(main_env) ) {
1.1       noro      702:                        read_exec_file = 1;
                    703:                        read_eval_loop();
                    704:                        read_exec_file = 0;
                    705:                }
                    706:                fclose(ifp);
                    707:        }
                    708:        input_init(0,"string");
                    709: #if !MPI
                    710:        ox_io_init();
                    711: #endif
                    712:        create_my_mathcap("ox_asir");
                    713: }
                    714:
1.20      noro      715: void ox_io_init() {
1.1       noro      716:        unsigned char c,rc;
1.21      noro      717:        extern int I_am_server;
1.26    ! noro      718:
        !           719:        /* XXX : ssh forwards stdin to a remote host */
        !           720:        fclose(stdin);
1.1       noro      721:
1.21      noro      722:        I_am_server = 1;
1.1       noro      723:        endian_init();
                    724: #if defined(VISUAL)
                    725:        if ( !ox_sock_id )
                    726:                exit(0);
                    727:        iofp[0].in = WSIO_open(ox_sock_id,"r");
                    728:        iofp[0].out = WSIO_open(ox_sock_id,"w");
                    729: #else
                    730:        iofp[0].in = fdopen(3,"r");
                    731:        iofp[0].out = fdopen(4,"w");
                    732:
                    733:        setbuffer(iofp[0].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    734:        setbuffer(iofp[0].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
                    735:        signal(SIGUSR1,ox_usr1_handler);
                    736: #endif
                    737:        asir_OperandStackSize = BUFSIZ;
                    738:        asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
                    739:        asir_OperandStackPtr = -1;
                    740:        if ( little_endian )
                    741:                c = 1;
                    742:        else
                    743:                c = 0xff;
                    744:        /* server : write -> read */
                    745:        write_char(iofp[0].out,&c); ox_flush_stream_force(0);
                    746:        read_char(iofp[0].in,&rc);
                    747:        iofp[0].conv = c == rc ? 0 : 1;
1.14      noro      748:        /* XXX; for raw I/O */
                    749:        register_server(0,0,0);
1.3       noro      750: }
                    751:
1.17      noro      752: #if !defined(VISUAL)
1.3       noro      753: /*
                    754:  * Library mode functions
                    755:  */
                    756:
                    757: /*
                    758:  * Converts a binary encoded CMO into a risa object
                    759:  * and pushes it onto the stack.
                    760:  */
                    761:
                    762: void asir_ox_push_cmo(void *cmo)
                    763: {
                    764:        Obj obj;
                    765:
                    766:        ox_copy_init(cmo);
                    767:        ox_buf_to_obj_as_cmo(&obj);
                    768:        asir_push_one(obj);
                    769: }
                    770:
                    771: /*
                    772:  * Pop an object from the stack and converts it
                    773:  * int a binary encoded CMO.
                    774:  */
                    775:
                    776: int asir_ox_pop_cmo(void *cmo, int limit)
                    777: {
                    778:        Obj obj;
                    779:        int len;
1.11      noro      780:        ERR err;
1.3       noro      781:
                    782:        obj = asir_pop_one();
1.10      noro      783:        if ( !valid_as_cmo(obj) ) {
1.11      noro      784:                asir_push_one(obj);
                    785:                create_error(&err,0,"The object at the stack top is invalid as a CMO.");
                    786:                obj = (Obj)err;
1.10      noro      787:        }
1.3       noro      788:        len = count_as_cmo(obj);
                    789:        if ( len <= limit ) {
                    790:                ox_copy_init(cmo);
                    791:                ox_obj_to_buf_as_cmo(obj);
                    792:                return len;
                    793:        } else
                    794:                return -1;
                    795: }
                    796:
                    797: /*
                    798:  * Executes an SM command.
                    799:  */
                    800:
1.13      noro      801: void asir_ox_push_cmd(int cmd)
1.3       noro      802: {
1.7       noro      803:        int ret;
                    804:        ERR err;
                    805:        extern char LastError[];
                    806:
1.25      noro      807:        if ( ret = setjmp(main_env) ) {
1.12      noro      808:                asir_reset_handler();
1.7       noro      809:                if ( ret == 1 ) {
                    810:                        create_error(&err,0,LastError); /* XXX */
                    811:                        asir_push_one((Obj)err);
                    812:                }
1.12      noro      813:        } else {
                    814:                asir_save_handler();
                    815:                asir_set_handler();
1.7       noro      816:                asir_do_cmd(cmd,0);
1.12      noro      817:                asir_reset_handler();
                    818:        }
1.3       noro      819: }
                    820:
                    821: /*
                    822:  * Executes a string written in Asir.
                    823:  */
                    824:
                    825: void asir_ox_execute_string(char *s)
                    826: {
                    827:        STRING str;
1.8       noro      828:        int ret;
                    829:        ERR err;
                    830:        extern char LastError[];
1.3       noro      831:
                    832:        MKSTR(str,s);
                    833:        asir_push_one((Obj)str);
1.25      noro      834:        if ( ret = setjmp(main_env) ) {
1.12      noro      835:                asir_reset_handler();
1.8       noro      836:                if ( ret == 1 ) {
                    837:                        create_error(&err,0,LastError); /* XXX */
                    838:                        asir_push_one((Obj)err);
                    839:                }
1.12      noro      840:        } else {
                    841:                asir_save_handler();
                    842:                asir_set_handler();
1.8       noro      843:                asir_executeString();
1.12      noro      844:                asir_reset_handler();
                    845:        }
1.3       noro      846: }
                    847:
                    848: /*
                    849:  * Returns the size as a CMO of the object
                    850:  * at the top of the stack.
                    851:  */
                    852:
                    853: int asir_ox_peek_cmo_size()
                    854: {
                    855:        Obj obj;
                    856:        int len;
                    857:
                    858:        obj = asir_pop_one();
1.11      noro      859:        asir_push_one(obj);
1.10      noro      860:        if ( !valid_as_cmo(obj) ) {
                    861:                fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
                    862:                return 0;
                    863:        }
1.3       noro      864:        len = count_as_cmo(obj);
                    865:        return len;
                    866: }
                    867:
                    868: /*
                    869:  * Initialization.
1.11      noro      870:  * byteorder=0 => native
                    871:  *          =1 => network byte order
1.3       noro      872:  */
                    873:
1.13      noro      874: int asir_ox_init(int byteorder)
1.3       noro      875: {
                    876:        int tmp;
                    877:        char ifname[BUFSIZ];
                    878:        extern int GC_dont_gc;
                    879:        extern int read_exec_file;
                    880:        extern int do_asirrc;
                    881:        extern int do_server_in_X11;
                    882:        char *getenv();
                    883:        static ox_asir_initialized = 0;
                    884:        FILE *ifp;
                    885:
                    886: #if !defined(VISUAL) && !MPI
1.9       noro      887:        do_server_in_X11 = 0; /* XXX */
1.3       noro      888: #endif
                    889:        asir_save_handler();
                    890: #if PARI
                    891:        risa_pari_init();
                    892: #endif
                    893:        srandom((int)get_current_time());
                    894:
                    895:        rtime_init();
                    896:        env_init();
                    897:        endian_init();
                    898:        GC_init();
                    899: /*     process_args(argc,argv); */
                    900:        output_init();
                    901:        arf_init();
                    902:        nglob_init();
                    903:        glob_init();
                    904:        sig_init();
                    905:        tty_init();
                    906:        debug_init();
                    907:        pf_init();
                    908:        sysf_init();
                    909:        parif_init();
                    910: #if defined(VISUAL)
                    911:        init_socket();
                    912: #endif
                    913: #if defined(UINIT)
                    914:        reg_sysf();
                    915: #endif
                    916:        sprintf(ifname,"%s/.asirrc",getenv("HOME"));
                    917:        if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
                    918:                input_init(ifp,ifname);
1.25      noro      919:                if ( !setjmp(main_env) ) {
1.3       noro      920:                        read_exec_file = 1;
                    921:                        read_eval_loop();
                    922:                        read_exec_file = 0;
                    923:                }
                    924:                fclose(ifp);
                    925:        }
                    926:        input_init(0,"string");
                    927:
                    928:        asir_OperandStackSize = BUFSIZ;
                    929:        asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
                    930:        asir_OperandStackPtr = -1;
1.11      noro      931:        if ( little_endian && byteorder )
                    932:                lib_ox_need_conv = 1;
1.3       noro      933:        else
1.11      noro      934:                lib_ox_need_conv = 0;
1.3       noro      935:        do_message = 0;
1.11      noro      936:        create_my_mathcap("ox_asir");
1.12      noro      937:        asir_reset_handler();
1.13      noro      938:        return 0;
1.1       noro      939: }
1.17      noro      940: #endif

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