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