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

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

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