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

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

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