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

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

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