Annotation of OpenXM_contrib2/asir2000/io/ox_asir.c, Revision 1.21
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.21 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.20 2000/10/06 06:05:22 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: #if defined(THINK_C)
666: param_init();
667: #endif
668: StackBottom = &tmp + 1; /* XXX */
669: rtime_init();
670: env_init();
671: endian_init();
672: #if !defined(VISUAL) && !defined(THINK_C)
673: /* check_key(); */
674: #endif
675: GC_init();
676: process_args(--argc,++argv);
677: output_init();
678: arf_init();
679: nglob_init();
680: glob_init();
681: sig_init();
682: tty_init();
683: debug_init();
684: pf_init();
685: sysf_init();
686: parif_init();
687: #if defined(VISUAL)
688: init_socket();
689: #endif
690: #if defined(UINIT)
691: reg_sysf();
692: #endif
1.4 noro 693: /* if ASIR_CONFIG is set, execute it; else execute .asirrc */
694: if ( ptr = getenv("ASIR_CONFIG") )
695: strcpy(ifname,ptr);
696: else {
1.1 noro 697: #if defined(THINK_C)
1.4 noro 698: sprintf(ifname,"asirrc");
1.1 noro 699: #else
1.4 noro 700: homedir = getenv("HOME");
701: if ( !homedir ) {
702: char rootname[BUFSIZ];
703:
704: get_rootdir(rootname,sizeof(rootname));
705: homedir = rootname;
706: }
707: sprintf(ifname,"%s/.asirrc",homedir);
1.1 noro 708: #endif
1.4 noro 709: }
1.1 noro 710: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
711: input_init(ifp,ifname);
712: if ( !setjmp(env) ) {
713: read_exec_file = 1;
714: read_eval_loop();
715: read_exec_file = 0;
716: }
717: fclose(ifp);
718: }
719: input_init(0,"string");
720: #if !MPI
721: ox_io_init();
722: #endif
723: create_my_mathcap("ox_asir");
724: }
725:
1.20 noro 726: void ox_io_init() {
1.1 noro 727: unsigned char c,rc;
1.21 ! noro 728: extern int I_am_server;
1.1 noro 729:
1.21 ! noro 730: I_am_server = 1;
1.1 noro 731: endian_init();
732: #if defined(VISUAL)
733: if ( !ox_sock_id )
734: exit(0);
735: iofp[0].in = WSIO_open(ox_sock_id,"r");
736: iofp[0].out = WSIO_open(ox_sock_id,"w");
737: #else
738: iofp[0].in = fdopen(3,"r");
739: iofp[0].out = fdopen(4,"w");
740:
741: setbuffer(iofp[0].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
742: setbuffer(iofp[0].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
743: signal(SIGUSR1,ox_usr1_handler);
744: #endif
745: asir_OperandStackSize = BUFSIZ;
746: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
747: asir_OperandStackPtr = -1;
748: if ( little_endian )
749: c = 1;
750: else
751: c = 0xff;
752: /* server : write -> read */
753: write_char(iofp[0].out,&c); ox_flush_stream_force(0);
754: read_char(iofp[0].in,&rc);
755: iofp[0].conv = c == rc ? 0 : 1;
1.14 noro 756: /* XXX; for raw I/O */
757: register_server(0,0,0);
1.3 noro 758: }
759:
1.17 noro 760: #if !defined(VISUAL)
1.3 noro 761: /*
762: * Library mode functions
763: */
764:
765: /*
766: * Converts a binary encoded CMO into a risa object
767: * and pushes it onto the stack.
768: */
769:
770: void asir_ox_push_cmo(void *cmo)
771: {
772: Obj obj;
773:
774: ox_copy_init(cmo);
775: ox_buf_to_obj_as_cmo(&obj);
776: asir_push_one(obj);
777: }
778:
779: /*
780: * Pop an object from the stack and converts it
781: * int a binary encoded CMO.
782: */
783:
784: int asir_ox_pop_cmo(void *cmo, int limit)
785: {
786: Obj obj;
787: int len;
1.11 noro 788: ERR err;
1.3 noro 789:
790: obj = asir_pop_one();
1.10 noro 791: if ( !valid_as_cmo(obj) ) {
1.11 noro 792: asir_push_one(obj);
793: create_error(&err,0,"The object at the stack top is invalid as a CMO.");
794: obj = (Obj)err;
1.10 noro 795: }
1.3 noro 796: len = count_as_cmo(obj);
797: if ( len <= limit ) {
798: ox_copy_init(cmo);
799: ox_obj_to_buf_as_cmo(obj);
800: return len;
801: } else
802: return -1;
803: }
804:
805: /*
806: * Executes an SM command.
807: */
808:
1.13 noro 809: void asir_ox_push_cmd(int cmd)
1.3 noro 810: {
1.7 noro 811: int ret;
812: ERR err;
813: extern char LastError[];
814:
815: if ( ret = setjmp(env) ) {
1.12 noro 816: asir_reset_handler();
1.7 noro 817: if ( ret == 1 ) {
818: create_error(&err,0,LastError); /* XXX */
819: asir_push_one((Obj)err);
820: }
1.12 noro 821: } else {
822: asir_save_handler();
823: asir_set_handler();
1.7 noro 824: asir_do_cmd(cmd,0);
1.12 noro 825: asir_reset_handler();
826: }
1.3 noro 827: }
828:
829: /*
830: * Executes a string written in Asir.
831: */
832:
833: void asir_ox_execute_string(char *s)
834: {
835: STRING str;
1.8 noro 836: int ret;
837: ERR err;
838: extern char LastError[];
1.3 noro 839:
840: MKSTR(str,s);
841: asir_push_one((Obj)str);
1.8 noro 842: if ( ret = setjmp(env) ) {
1.12 noro 843: asir_reset_handler();
1.8 noro 844: if ( ret == 1 ) {
845: create_error(&err,0,LastError); /* XXX */
846: asir_push_one((Obj)err);
847: }
1.12 noro 848: } else {
849: asir_save_handler();
850: asir_set_handler();
1.8 noro 851: asir_executeString();
1.12 noro 852: asir_reset_handler();
853: }
1.3 noro 854: }
855:
856: /*
857: * Returns the size as a CMO of the object
858: * at the top of the stack.
859: */
860:
861: int asir_ox_peek_cmo_size()
862: {
863: Obj obj;
864: int len;
865:
866: obj = asir_pop_one();
1.11 noro 867: asir_push_one(obj);
1.10 noro 868: if ( !valid_as_cmo(obj) ) {
869: fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
870: return 0;
871: }
1.3 noro 872: len = count_as_cmo(obj);
873: return len;
874: }
875:
876: /*
877: * Initialization.
1.11 noro 878: * byteorder=0 => native
879: * =1 => network byte order
1.3 noro 880: */
881:
1.13 noro 882: int asir_ox_init(int byteorder)
1.3 noro 883: {
884: int tmp;
885: char ifname[BUFSIZ];
886: extern int GC_dont_gc;
887: extern int read_exec_file;
888: extern int do_asirrc;
889: extern int do_server_in_X11;
890: char *getenv();
891: static ox_asir_initialized = 0;
892: FILE *ifp;
893:
894: #if !defined(VISUAL) && !MPI
1.9 noro 895: do_server_in_X11 = 0; /* XXX */
1.3 noro 896: #endif
897: asir_save_handler();
898: #if PARI
899: risa_pari_init();
900: #endif
901: srandom((int)get_current_time());
902:
903: #if defined(THINK_C)
904: param_init();
905: #endif
906: StackBottom = &tmp + 1; /* XXX */
907: rtime_init();
908: env_init();
909: endian_init();
910: #if !defined(VISUAL) && !defined(THINK_C)
911: /* check_key(); */
912: #endif
913: GC_init();
914: /* process_args(argc,argv); */
915: output_init();
916: arf_init();
917: nglob_init();
918: glob_init();
919: sig_init();
920: tty_init();
921: debug_init();
922: pf_init();
923: sysf_init();
924: parif_init();
925: #if defined(VISUAL)
926: init_socket();
927: #endif
928: #if defined(UINIT)
929: reg_sysf();
930: #endif
931: #if defined(THINK_C)
932: sprintf(ifname,"asirrc");
933: #else
934: sprintf(ifname,"%s/.asirrc",getenv("HOME"));
935: #endif
936: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
937: input_init(ifp,ifname);
938: if ( !setjmp(env) ) {
939: read_exec_file = 1;
940: read_eval_loop();
941: read_exec_file = 0;
942: }
943: fclose(ifp);
944: }
945: input_init(0,"string");
946:
947: asir_OperandStackSize = BUFSIZ;
948: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
949: asir_OperandStackPtr = -1;
1.11 noro 950: if ( little_endian && byteorder )
951: lib_ox_need_conv = 1;
1.3 noro 952: else
1.11 noro 953: lib_ox_need_conv = 0;
1.3 noro 954: do_message = 0;
1.11 noro 955: create_my_mathcap("ox_asir");
1.12 noro 956: asir_reset_handler();
1.13 noro 957: return 0;
1.1 noro 958: }
1.17 noro 959: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>