Annotation of OpenXM_contrib2/asir2000/io/ox_asir.c, Revision 1.64
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.64 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.63 2006/06/21 09:46:06 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.41 ohara 54: #if defined(PARI)
1.6 noro 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:
1.36 noro 61: /* environement is defined in libpari.a */
1.51 saito 62: # if !( PARI_VERSION_CODE > 131588)
1.36 noro 63: extern jmp_buf environnement;
1.51 saito 64: # endif
1.45 noro 65: extern int myrank_102,nserver_102;
1.1 noro 66:
67: extern int do_message;
68: extern int ox_flushing;
1.35 noro 69: extern JMP_BUF ox_env;
1.1 noro 70: extern MATHCAP my_mathcap;
71:
1.11 noro 72: extern int little_endian,ox_sock_id;
1.59 noro 73: extern char LastError[];
74: extern LIST LastStackTrace;
1.11 noro 75:
1.1 noro 76: int ox_sock_id;
1.11 noro 77: int lib_ox_need_conv;
1.1 noro 78:
1.59 noro 79: void create_error(ERR *,unsigned int ,char *,LIST trace);
1.19 noro 80:
1.20 noro 81: int asir_OperandStackSize;
82: Obj *asir_OperandStack;
83: int asir_OperandStackPtr = -1;
84:
85: void ox_io_init();
1.40 noro 86: void ox_asir_init(int,char **,char *);
1.20 noro 87: Obj asir_pop_one();
88: Obj asir_peek_one();
89: void asir_push_one(Obj);
90: void asir_end_flush();
91: int asir_executeString();
92: void asir_evalName(unsigned int);
93: void asir_setName(unsigned int);
94: void asir_pops();
95: void asir_popString();
96: void asir_popCMO(unsigned int);
97: void asir_popSerializedLocalObject();
98: void asir_pushCMOtag(unsigned int);
1.45 noro 99: void asir_set_rank_102(unsigned int);
100: void asir_tcp_accept_102(unsigned int);
101: void asir_tcp_connect_102(unsigned int);
102: void asir_reset_102(unsigned int serial);
1.47 noro 103: void asir_bcast_102(unsigned int serial);
104: void asir_reduce_102(unsigned int serial);
1.20 noro 105: LIST asir_GetErrorList();
106: char *name_of_cmd(int);
107: char *name_of_id(int);
108:
109: static void asir_do_cmd(int,unsigned int);
1.1 noro 110: static void asir_executeFunction(int);
111:
1.43 noro 112: #if defined(MPI)
1.14 noro 113: /* XXX : currently MPI version supports only a homogeneous cluster. */
114:
1.1 noro 115: extern int mpi_nprocs,mpi_myid;
116:
117: void ox_mpi_master_init() {
1.14 noro 118: int i,idx;
1.1 noro 119:
1.14 noro 120: for ( i = 0; i < mpi_nprocs; i++ ) {
121: /* ordering information is not exchanged */
122: /* idx should be equal to i */
1.1 noro 123: idx = get_iofp(i,0,0);
1.14 noro 124: register_server(0,idx,idx);
1.1 noro 125: }
126: }
127:
128: void ox_mpi_slave_init() {
1.14 noro 129: int i,idx;
130:
1.1 noro 131: endian_init();
132: fclose(stdin);
1.14 noro 133: for ( i = 0; i < mpi_nprocs; i++ ) {
134: /* ordering information is not exchanged */
135: /* idx should be equal to i */
136: idx = get_iofp(i,0,0);
137: register_server(0,idx,idx);
138: }
1.1 noro 139: asir_OperandStackSize = BUFSIZ;
140: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
141: asir_OperandStackPtr = -1;
142: }
143: #endif
144:
145: void ox_main(int argc,char **argv) {
146: int id;
1.13 noro 147: int cmd;
1.1 noro 148: Obj obj;
149: ERR err;
150: unsigned int serial;
151: int ret;
152:
1.40 noro 153: ox_asir_init(argc,argv,"ox_asir");
1.1 noro 154: if ( do_message )
155: fprintf(stderr,"I'm an ox_asir, Version %d.\n",ASIR_VERSION);
1.35 noro 156: if ( SETJMP(ox_env) ) {
1.1 noro 157: while ( NEXT(asir_infile) )
158: closecurrentinput();
1.56 noro 159: resetpvs();
160: reset_engine();
161: reset_io();
1.1 noro 162: ox_send_sync(0);
163: }
164: while ( 1 ) {
165: extern int recv_intr;
166:
167: serial = ox_recv(0,&id,&obj);
168: #if defined(VISUAL)
169: if ( recv_intr ) {
170: if ( recv_intr == 1 ) {
171: recv_intr = 0;
172: int_handler(SIGINT);
173: } else {
174: recv_intr = 0;
175: ox_usr1_handler(0);
176: }
177: }
178: #endif
179: if ( do_message )
180: fprintf(stderr,"#%d Got %s",serial,name_of_id(id));
181: switch ( id ) {
182: case OX_COMMAND:
183: cmd = ((USINT)obj)->body;
184: if ( ox_flushing )
185: break;
186: if ( do_message )
187: fprintf(stderr," %s\n",name_of_cmd(cmd));
1.35 noro 188: if ( ret = SETJMP(main_env) ) {
1.1 noro 189: if ( ret == 1 ) {
1.59 noro 190: create_error(&err,serial,LastError,LastStackTrace);
1.1 noro 191: asir_push_one((Obj)err);
1.54 noro 192: while ( NEXT(asir_infile) )
193: closecurrentinput();
1.63 noro 194: resetpvs();
1.1 noro 195: }
196: break;
197: }
198: asir_do_cmd(cmd,serial);
199: break;
200: case OX_DATA:
201: case OX_LOCAL_OBJECT_ASIR:
202: if ( ox_flushing )
203: break;
204: if ( do_message )
205: fprintf(stderr," -> data pushed");
206: asir_push_one(obj);
207: break;
208: case OX_SYNC_BALL:
209: asir_end_flush();
210: break;
211: default:
212: break;
213: }
214: if ( do_message )
215: fprintf(stderr,"\n");
216: }
217: }
218:
1.13 noro 219: static void asir_do_cmd(int cmd,unsigned int serial)
1.1 noro 220: {
221: MATHCAP client_mathcap;
222: Q q;
223: int i;
224: LIST list;
225:
226: switch ( cmd ) {
227: case SM_dupErrors:
228: list = asir_GetErrorList();
229: asir_push_one((Obj)list);
230: break;
231: case SM_getsp:
232: i = asir_OperandStackPtr+1;
233: STOQ(i,q);
234: asir_push_one((Obj)q);
235: break;
236: case SM_popSerializedLocalObject:
237: asir_popSerializedLocalObject();
238: break;
239: case SM_popCMO:
240: asir_popCMO(serial);
241: break;
242: case SM_popString:
243: asir_popString();
244: break;
245: case SM_setName:
246: asir_setName(serial);
247: break;
248: case SM_evalName:
249: asir_evalName(serial);
250: break;
251: case SM_executeStringByLocalParser:
252: asir_executeString();
253: break;
254: case SM_executeStringByLocalParserInBatchMode:
255: asir_executeString();
256: asir_pop_one();
257: break;
258: case SM_executeFunction:
259: asir_executeFunction(serial);
260: break;
261: case SM_shutdown:
262: asir_terminate(2);
263: break;
264: case SM_pops:
265: asir_pops();
266: break;
267: case SM_mathcap:
268: asir_push_one((Obj)my_mathcap);
269: break;
270: case SM_setMathcap:
271: client_mathcap = (MATHCAP)asir_pop_one();
272: store_remote_mathcap(0,client_mathcap);
273: break;
1.18 noro 274: case SM_pushCMOtag:
275: asir_pushCMOtag(serial);
276: break;
1.45 noro 277: case SM_set_rank_102:
278: asir_set_rank_102(serial);
279: break;
280: case SM_tcp_accept_102:
281: asir_tcp_accept_102(serial);
282: break;
283: case SM_tcp_connect_102:
284: asir_tcp_connect_102(serial);
285: break;
286: case SM_reset_102:
287: asir_reset_102(serial);
288: break;
1.47 noro 289: case SM_bcast_102:
290: asir_bcast_102(serial);
291: break;
292: case SM_reduce_102:
293: asir_reduce_102(serial);
294: break;
1.1 noro 295: case SM_nop:
296: default:
297: break;
298: }
299: }
300:
1.20 noro 301: char *name_of_id(int id)
1.1 noro 302: {
303: switch ( id ) {
304: case OX_COMMAND:
305: return "OX_COMMAND";
306: break;
307: case OX_DATA:
308: return "OX_DATA";
309: break;
310: case OX_LOCAL_OBJECT_ASIR:
311: return "OX_LOCAL_OBJECT_ASIR";
312: break;
313: case OX_SYNC_BALL:
314: return "OX_SYNC_BALL";
315: break;
316: default:
317: return "Unknown id";
318: break;
319: }
320: }
321:
1.20 noro 322: char *name_of_cmd(int cmd)
1.1 noro 323: {
324: switch ( cmd ) {
325: case SM_popSerializedLocalObject:
326: return "SM_popSerializedLocalObject";
327: break;
328: case SM_popCMO:
329: return "SM_popCMO";
330: break;
331: case SM_popString:
332: return "SM_popString";
333: break;
334: case SM_pops:
335: return "SM_pops";
336: break;
337: case SM_setName:
338: return "SM_setName";
339: break;
340: case SM_evalName:
341: return "SM_evalName";
342: break;
343: case SM_executeStringByLocalParser:
344: return "SM_executeString";
345: break;
346: case SM_executeFunction:
347: return "SM_executeFunction";
348: break;
349: case SM_shutdown:
350: return "SM_shutdown";
351: break;
352: case SM_beginBlock:
353: return "SM_beginBlock";
354: break;
355: case SM_endBlock:
356: return "SM_endBlock";
357: break;
358: case SM_mathcap:
359: return "SM_mathcap";
360: break;
361: case SM_setMathcap:
362: return "SM_setMathcap";
363: break;
364: case SM_getsp:
365: return "SM_setMathcap";
366: break;
367: case SM_dupErrors:
368: return "SM_dupErrors";
369: break;
370: case SM_nop:
371: return "SM_nop";
1.18 noro 372: case SM_pushCMOtag:
373: return "SM_pushCMOtag";
1.45 noro 374: case SM_set_rank_102:
375: return "SM_set_rank_102";
376: break;
377: case SM_tcp_accept_102:
378: return "SM_tcp_accept_102";
379: break;
380: case SM_tcp_connect_102:
381: return "SM_tcp_connect_102";
382: case SM_reset_102:
383: return "SM_reset_102";
384: break;
1.47 noro 385: case SM_bcast_102:
386: return "SM_bcast_102";
387: break;
388: case SM_reduce_102:
389: return "SM_reduce_102";
390: break;
1.1 noro 391: default:
392: return "Unknown cmd";
393: break;
394: }
395: }
396:
1.20 noro 397: LIST asir_GetErrorList()
1.1 noro 398: {
399: int i;
400: NODE n,n0;
401: LIST err;
402: Obj obj;
403:
404: for ( i = 0, n0 = 0; i <= asir_OperandStackPtr; i++ )
405: if ( (obj = asir_OperandStack[i]) && (OID(obj) == O_ERR) ) {
406: NEXTNODE(n0,n); BDY(n) = (pointer)obj;
407: }
408: if ( n0 )
409: NEXT(n) = 0;
410: MKLIST(err,n0);
411: return err;
412: }
413:
1.20 noro 414: void asir_popSerializedLocalObject()
1.1 noro 415: {
416: Obj obj;
417: VL t,vl;
418:
419: obj = asir_pop_one();
420: get_vars_recursive(obj,&vl);
421: for ( t = vl; t; t = NEXT(t) )
422: if ( t->v->attr == (pointer)V_UC )
423: error("bsave : not implemented");
424: ox_send_cmd(0,SM_beginBlock);
425: ox_send_local_ring(0,vl);
426: ox_send_local_data(0,obj);
427: ox_send_cmd(0,SM_endBlock);
428: }
429:
1.20 noro 430: void asir_popCMO(unsigned int serial)
1.1 noro 431: {
432: Obj obj;
433: ERR err;
434:
435: obj = asir_pop_one();
436: if ( valid_as_cmo(obj) )
437: ox_send_data(0,obj);
438: else {
1.59 noro 439: create_error(&err,serial,"cannot convert to CMO object",0);
1.1 noro 440: ox_send_data(0,err);
441: asir_push_one(obj);
1.45 noro 442: }
443: }
444:
1.47 noro 445: void asir_reduce_102(unsigned int serial)
446: {
447: Q r;
448: int root;
449: Obj data,obj;
450: ERR err;
451: STRING op;
452: char *opname;
453: void (*func)();
454:
455: func = 0;
456: op = (STRING)asir_pop_one();
457: opname = BDY(op);
458: r = (Q)asir_pop_one();
459: root = QTOS(r);
460: if ( !strcmp(opname,"+") )
461: func = arf_add;
462: else if ( !strcmp(opname,"*") )
463: func = arf_mul;
464: if ( !func ) {
1.59 noro 465: create_error(&err,serial,"Invalid opration in ox_reduce_102",0);
1.47 noro 466: asir_push_one(obj);
467: } else
468: ox_reduce_102(root,func);
469: }
470:
471: void asir_bcast_102(unsigned int serial)
472: {
473: Q r;
474: int root;
475: Obj data;
476:
477: r = (Q)asir_pop_one();
478: root = QTOS(r);
479: ox_bcast_102(root);
480: }
481:
1.45 noro 482: void asir_reset_102(unsigned int serial)
483: {
484: int i,j,id;
485: Obj obj;
486:
487: for ( i = 0; i < myrank_102; i++ )
488: do {
489: ox_recv_102(i,&id,&obj);
490: } while ( id != OX_SYNC_BALL );
1.50 noro 491: for ( i = myrank_102+1; i < nserver_102; i++ )
1.45 noro 492: ox_send_sync_102(i);
493: }
494:
495: void asir_set_rank_102(unsigned int serial)
496: {
497: Obj obj;
498: Q rank,nserver;
499: int n,r,stat;
500: NODE arg;
501: ERR err;
502:
503: rank = (Q)asir_pop_one();
504: nserver = (Q)asir_pop_one();
505: stat = 0;
506: if ( !nserver || !INT(nserver) || !INT(rank) ) {
507: stat = -1;
508: } else {
509: n = QTOS(nserver); r = QTOS(rank);
510: if ( n <= 0 || r < 0 || r >= n ) {
511: stat = -1;
512: }
513: myrank_102 = r;
514: nserver_102 = n;
515: }
516: if ( !stat ) return;
517: else {
1.59 noro 518: create_error(&err,serial,"Invalid argument(s) in ox_set_rank_102",0);
1.45 noro 519: asir_push_one(obj);
520: }
521: }
522:
523: void asir_tcp_accept_102(unsigned int serial)
524: {
525: Obj obj;
1.46 noro 526: Q r,p;
1.45 noro 527: ERR err;
1.46 noro 528: char port_str[BUFSIZ];
529: int port,s,use_unix,rank;
1.45 noro 530:
1.46 noro 531: r = (Q)asir_pop_one();
532: p = (Q)asir_pop_one();
533: if ( IS_CYGWIN || !p || NUM(p) ) {
534: port = QTOS(p);
535: sprintf(port_str,"%d",port);
536: use_unix = 0;
537: } else {
538: strcpy(port_str,BDY((STRING)p));
539: use_unix = 1;
540: }
541: s = try_bind_listen(use_unix,port_str);
542: s = try_accept(use_unix,s);
543: rank = QTOS((Q)r);
544: if ( register_102(s,rank,1) < 0 ) {
545: create_error(&err,serial,
1.59 noro 546: "failed to bind or accept in ox_tcp_accept_102",0);
1.45 noro 547: asir_push_one((Obj)err);
548: }
549: }
550:
551: void asir_tcp_connect_102(unsigned int serial)
552: {
553: Obj obj;
1.46 noro 554: Q r,p;
555: STRING h;
1.45 noro 556: ERR err;
1.46 noro 557: char *host;
558: char port_str[BUFSIZ];
559: int port,s,use_unix,rank;
560:
561: r = (Q)asir_pop_one();
562: p = (Q)asir_pop_one();
563: h = (STRING)asir_pop_one();
564: if ( IS_CYGWIN || !p || NUM(p) ) {
565: port = QTOS(p);
566: sprintf(port_str,"%d",port);
567: use_unix = 0;
1.49 noro 568: host = BDY((STRING)h);
1.46 noro 569: } else {
570: strcpy(port_str,BDY((STRING)p));
571: use_unix = 1;
1.49 noro 572: host = 0;
1.46 noro 573: }
574: s = try_connect(use_unix,host,port_str);
575: rank = QTOS((Q)r);
576: if ( register_102(s,rank,1) < 0 ) {
577: create_error(&err,serial,
1.59 noro 578: "failed to connect in ox_tcp_connect_102",0);
1.45 noro 579: asir_push_one((Obj)err);
1.1 noro 580: }
581: }
582:
1.20 noro 583: void asir_pushCMOtag(unsigned int serial)
1.18 noro 584: {
585: Obj obj;
586: ERR err;
587: USINT ui;
588: int tag;
589:
590: obj = asir_peek_one();
591: if ( cmo_tag(obj,&tag) ) {
592: MKUSINT(ui,tag);
593: asir_push_one((Obj)ui);
594: } else {
1.59 noro 595: create_error(&err,serial,"cannot convert to CMO object",0);
1.18 noro 596: asir_push_one((Obj)err);
597: }
598: }
599:
1.64 ! noro 600: void print_to_wfep(Obj obj)
! 601: {
! 602: asir_push_one(obj);
! 603: asir_popString();
! 604: }
! 605:
1.20 noro 606: void asir_popString()
1.1 noro 607: {
608: Obj val;
1.53 noro 609: char *buf;
1.1 noro 610: int l;
611: STRING str;
612:
613: val = asir_pop_one();
614: if ( !val )
1.53 noro 615: buf = "0";
1.1 noro 616: else {
617: l = estimate_length(CO,val);
618: buf = (char *)ALLOCA(l+1);
619: soutput_init(buf);
620: sprintexpr(CO,val);
621: }
1.52 noro 622: MKSTR(str,buf);
1.1 noro 623: ox_send_data(0,str);
624: }
625:
1.20 noro 626: void asir_pops()
1.1 noro 627: {
628: int n;
629:
630: n = (int)(((USINT)asir_pop_one())->body);
631: asir_OperandStackPtr = MAX(asir_OperandStackPtr-n,-1);
632: }
633:
1.20 noro 634: void asir_setName(unsigned int serial)
1.1 noro 635: {
636: char *name;
637: int l,n;
638: char *dummy = "=0;";
639: SNODE snode;
640: ERR err;
641:
642: name = ((STRING)asir_pop_one())->body;
643: l = strlen(name);
644: n = l+strlen(dummy)+1;
645: parse_strp = (char *)ALLOCA(n);
646: sprintf(parse_strp,"%s%s",name,dummy);
647: if ( mainparse(&snode) ) {
1.59 noro 648: create_error(&err,serial,"cannot set to variable",0);
1.1 noro 649: asir_push_one((Obj)err);
650: } else {
651: FA1((FNODE)FA0(snode)) = (pointer)mkfnode(1,I_FORMULA,asir_pop_one());
652: evalstat(snode);
653: }
654: }
655:
1.20 noro 656: void asir_evalName(unsigned int serial)
1.1 noro 657: {
658: char *name;
659: int l,n;
660: SNODE snode;
661: ERR err;
662: pointer val;
663:
664: name = ((STRING)asir_pop_one())->body;
665: l = strlen(name);
666: n = l+2;
667: parse_strp = (char *)ALLOCA(n);
668: sprintf(parse_strp,"%s;",name);
669: if ( mainparse(&snode) ) {
1.59 noro 670: create_error(&err,serial,"no such variable",0);
1.1 noro 671: val = (pointer)err;
672: } else
673: val = evalstat(snode);
674: asir_push_one(val);
675: }
676:
1.55 noro 677: char *augment_backslash(char *s)
678: {
679: char *p,*r;
680: int i;
681:
682: for ( i = 0, p = s; *p; p++, i++ ) if ( *p == '\\' ) i++;
683: r = (char *)MALLOC_ATOMIC((i+1)*sizeof(char));
684: for ( i = 0, p = s; *p; p++, i++ ) {
685: if ( *p == '\\' ) r[i++] = '\\';
686: r[i] = *p;
687: }
688: return r;
689: }
690:
1.20 noro 691: int asir_executeString()
1.1 noro 692: {
693: SNODE snode;
694: pointer val;
695: char *cmd;
1.41 ohara 696: #if defined(PARI)
1.1 noro 697: recover(0);
1.36 noro 698: /* environement is defined in libpari.a */
1.51 saito 699: # if !(PARI_VERSION_CODE > 131588 )
1.36 noro 700: if ( setjmp(environnement) ) {
1.1 noro 701: avma = top; recover(1);
702: resetenv("");
703: }
1.51 saito 704: # endif
1.1 noro 705: #endif
706: cmd = ((STRING)asir_pop_one())->body;
1.58 noro 707: /* XXX : probably this is useless */
708: #if 0
1.55 noro 709: parse_strp = augment_backslash(cmd);
1.58 noro 710: #else
711: parse_strp = cmd;
712: #endif
1.62 noro 713: asir_infile->ln = 1;
1.1 noro 714: if ( mainparse(&snode) ) {
715: return -1;
716: }
717: val = evalstat(snode);
718: if ( NEXT(asir_infile) ) {
719: while ( NEXT(asir_infile) ) {
720: if ( mainparse(&snode) ) {
721: asir_push_one(val);
722: return -1;
723: }
724: nextbp = 0;
725: val = evalstat(snode);
726: }
727: }
728: asir_push_one(val);
729: return 0;
730: }
731:
732: static void asir_executeFunction(int serial)
733: {
734: char *func;
735: int argc;
736: FUNC f;
737: Obj result;
738: NODE n,n1;
739: STRING fname;
740: char *path;
741: ERR err;
1.5 noro 742: Obj arg;
1.1 noro 743: static char buf[BUFSIZ];
744:
1.5 noro 745: arg = asir_pop_one();
746: if ( !arg || OID(arg) != O_STR ) {
747: sprintf(buf,"executeFunction : invalid function name");
748: goto error;
749: } else
750: func = ((STRING)arg)->body;
751:
752: arg = asir_pop_one();
753: if ( !arg || OID(arg) != O_USINT ) {
754: sprintf(buf,"executeFunction : invalid argc");
755: goto error;
756: } else
757: argc = (int)(((USINT)arg)->body);
1.1 noro 758:
759: for ( n = 0; argc; argc-- ) {
760: NEXTNODE(n,n1);
761: BDY(n1) = (pointer)asir_pop_one();
762: }
763: if ( n )
764: NEXT(n1) = 0;
765:
1.59 noro 766: #if 0
1.1 noro 767: if ( !strcmp(func,"load") ) {
768: fname = (STRING)BDY(n);
769: if ( OID(fname) == O_STR ) {
770: searchasirpath(BDY(fname),&path);
771: if ( path ) {
772: if ( do_message )
773: fprintf(stderr,"loading %s\n",path);
774: execasirfile(path);
775: } else
776: if ( do_message )
777: fprintf(stderr,"load : %s not found in the search path\n",BDY(fname));
778: }
779: result = 0;
780: } else {
1.59 noro 781: #endif
1.1 noro 782: searchf(noargsysf,func,&f);
783: if ( !f )
784: searchf(sysf,func,&f);
785: if ( !f )
786: searchf(ubinf,func,&f);
787: if ( !f )
788: searchf(usrf,func,&f);
789: if ( !f ) {
790: sprintf(buf,"executeFunction : the function %s not found",func);
1.5 noro 791: goto error;
1.1 noro 792: } else {
793: result = (Obj)bevalf(f,n);
794: }
1.59 noro 795: #if 0
1.1 noro 796: }
1.59 noro 797: #endif
1.5 noro 798: asir_push_one(result);
799: return;
800:
801: error:
1.59 noro 802: create_error(&err,serial,buf,0);
1.5 noro 803: result = (Obj)err;
1.1 noro 804: asir_push_one(result);
805: }
806:
1.20 noro 807: void asir_end_flush()
1.1 noro 808: {
809: ox_flushing = 0;
810: }
811:
812: /*
813: asir_OperandStackPtr points to the surface of the stack.
814: That is, the data at the stack top is
815: asir_OperandStack[asir_OperandStackPtr].
816: */
817:
818:
1.20 noro 819: void asir_push_one(Obj obj)
1.1 noro 820: {
821: if ( !obj || OID(obj) != O_VOID ) {
822: asir_OperandStackPtr++;
823: if ( asir_OperandStackPtr >= asir_OperandStackSize ) {
824: asir_OperandStackSize += BUFSIZ;
825: asir_OperandStack
826: = (Obj *)REALLOC(asir_OperandStack,
827: asir_OperandStackSize*sizeof(Obj));
828: }
829: asir_OperandStack[asir_OperandStackPtr] = obj;
830: }
831: }
832:
1.20 noro 833: Obj asir_pop_one() {
1.1 noro 834: if ( asir_OperandStackPtr < 0 ) {
835: if ( do_message )
836: fprintf(stderr,"OperandStack underflow");
837: return 0;
838: } else {
839: if ( do_message )
840: fprintf(stderr,"pop at %d\n",asir_OperandStackPtr);
841: return asir_OperandStack[asir_OperandStackPtr--];
1.18 noro 842: }
843: }
844:
1.20 noro 845: Obj asir_peek_one() {
1.18 noro 846: if ( asir_OperandStackPtr < 0 ) {
847: if ( do_message )
848: fprintf(stderr,"OperandStack underflow");
849: return 0;
850: } else {
851: if ( do_message )
852: fprintf(stderr,"peek at %d\n",asir_OperandStackPtr);
853: return asir_OperandStack[asir_OperandStackPtr];
1.1 noro 854: }
855: }
856:
1.40 noro 857: void ox_asir_init(int argc,char **argv,char *servername)
1.1 noro 858: {
859: char ifname[BUFSIZ];
860: extern int GC_dont_gc;
861: extern int do_asirrc;
862: extern int do_server_in_X11;
1.37 noro 863: extern char displayname[];
1.1 noro 864: char *getenv();
865: static ox_asir_initialized = 0;
866: FILE *ifp;
1.4 noro 867: char *homedir;
868: char *ptr;
1.61 noro 869: int do_server_sav;
1.32 noro 870: #if !defined(VISUAL)
871: int tmp;
872: #endif
1.1 noro 873:
1.57 noro 874: GC_init();
1.43 noro 875: #if !defined(VISUAL) && !defined(MPI)
1.1 noro 876: do_server_in_X11 = 1; /* XXX */
877: #endif
878: asir_save_handler();
1.41 ohara 879: #if defined(PARI)
1.1 noro 880: risa_pari_init();
881: #endif
882: srandom((int)get_current_time());
883:
884: rtime_init();
885: env_init();
886: endian_init();
1.44 noro 887: cppname_init();
1.1 noro 888: process_args(--argc,++argv);
1.37 noro 889: #if defined(__CYGWIN__)
890: if ( !displayname[0] )
891: do_server_in_X11 = 0; /* XXX */
892: #endif
1.1 noro 893: output_init();
894: arf_init();
895: nglob_init();
896: glob_init();
897: sig_init();
898: tty_init();
899: debug_init();
900: pf_init();
901: sysf_init();
902: parif_init();
903: #if defined(VISUAL)
904: init_socket();
905: #endif
906: #if defined(UINIT)
907: reg_sysf();
908: #endif
1.4 noro 909: /* if ASIR_CONFIG is set, execute it; else execute .asirrc */
910: if ( ptr = getenv("ASIR_CONFIG") )
911: strcpy(ifname,ptr);
912: else {
913: homedir = getenv("HOME");
914: if ( !homedir ) {
915: char rootname[BUFSIZ];
916:
917: get_rootdir(rootname,sizeof(rootname));
918: homedir = rootname;
919: }
920: sprintf(ifname,"%s/.asirrc",homedir);
921: }
1.59 noro 922:
923: /* the bottom of the input stack */
924: input_init(0,"string");
925:
1.1 noro 926: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
927: fclose(ifp);
1.61 noro 928: do_server_sav = do_server_in_X11;
929: do_server_in_X11 = 0;
1.60 noro 930: if ( !SETJMP(main_env) )
931: execasirfile(ifname);
1.61 noro 932: do_server_in_X11 = do_server_sav;
1.1 noro 933: }
1.59 noro 934:
1.30 noro 935: /* XXX Windows compatibility */
936: ox_io_init();
1.40 noro 937: create_my_mathcap(servername);
1.1 noro 938: }
939:
1.20 noro 940: void ox_io_init() {
1.1 noro 941: unsigned char c,rc;
1.21 noro 942: extern int I_am_server;
1.26 noro 943:
1.33 noro 944: /* XXX : ssh forwards stdin to a remote host on PC Unix */
945: #if defined(linux)
946: #include <sys/param.h>
1.32 noro 947: int i;
948:
949: close(0);
950: for ( i = 5; i < NOFILE; i++ )
951: close(i);
1.33 noro 952: #elif defined(__FreeBSD__)
1.27 noro 953: #include <sys/resource.h>
1.33 noro 954: int i;
1.32 noro 955: struct rlimit rl;
1.27 noro 956:
1.32 noro 957: getrlimit(RLIMIT_NOFILE,&rl);
958: close(0);
959: for ( i = 5; i < rl.rlim_cur; i++ )
960: close(i);
1.27 noro 961: #endif
1.1 noro 962:
1.21 noro 963: I_am_server = 1;
1.1 noro 964: endian_init();
965: #if defined(VISUAL)
966: if ( !ox_sock_id )
967: exit(0);
968: iofp[0].in = WSIO_open(ox_sock_id,"r");
969: iofp[0].out = WSIO_open(ox_sock_id,"w");
970: #else
971: iofp[0].in = fdopen(3,"r");
972: iofp[0].out = fdopen(4,"w");
973:
1.34 noro 974: #if !defined(__CYGWIN__)
1.1 noro 975: setbuffer(iofp[0].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
976: setbuffer(iofp[0].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
1.34 noro 977: #endif
1.1 noro 978: signal(SIGUSR1,ox_usr1_handler);
979: #endif
980: asir_OperandStackSize = BUFSIZ;
981: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
982: asir_OperandStackPtr = -1;
983: if ( little_endian )
984: c = 1;
985: else
986: c = 0xff;
987: /* server : write -> read */
988: write_char(iofp[0].out,&c); ox_flush_stream_force(0);
989: read_char(iofp[0].in,&rc);
990: iofp[0].conv = c == rc ? 0 : 1;
1.14 noro 991: /* XXX; for raw I/O */
992: register_server(0,0,0);
1.3 noro 993: }
994:
1.17 noro 995: #if !defined(VISUAL)
1.3 noro 996: /*
997: * Library mode functions
998: */
999:
1000: /*
1001: * Converts a binary encoded CMO into a risa object
1002: * and pushes it onto the stack.
1003: */
1004:
1005: void asir_ox_push_cmo(void *cmo)
1006: {
1007: Obj obj;
1008:
1009: ox_copy_init(cmo);
1010: ox_buf_to_obj_as_cmo(&obj);
1011: asir_push_one(obj);
1012: }
1013:
1014: /*
1015: * Pop an object from the stack and converts it
1.28 noro 1016: * into a binary encoded CMO.
1.3 noro 1017: */
1018:
1019: int asir_ox_pop_cmo(void *cmo, int limit)
1020: {
1021: Obj obj;
1022: int len;
1.11 noro 1023: ERR err;
1.3 noro 1024:
1025: obj = asir_pop_one();
1.10 noro 1026: if ( !valid_as_cmo(obj) ) {
1.11 noro 1027: asir_push_one(obj);
1.59 noro 1028: create_error(&err,0,"The object at the stack top is invalid as a CMO.",0);
1.11 noro 1029: obj = (Obj)err;
1.10 noro 1030: }
1.3 noro 1031: len = count_as_cmo(obj);
1032: if ( len <= limit ) {
1033: ox_copy_init(cmo);
1034: ox_obj_to_buf_as_cmo(obj);
1035: return len;
1036: } else
1037: return -1;
1.28 noro 1038: }
1039:
1040: int asir_ox_pop_string(void *string, int limit)
1041: {
1042: Obj val;
1043: int l;
1044:
1045: val = asir_pop_one();
1046: if ( !val ) {
1047: if ( limit >= 2 ) {
1048: sprintf(string,"0");
1049: l = strlen(string);
1050: } else
1051: l = -1;
1052: } else {
1053: l = estimate_length(CO,val);
1054: if ( l+1 <= limit ) {
1055: soutput_init(string);
1056: sprintexpr(CO,val);
1057: l = strlen(string);
1058: } else
1059: l = -1;
1060: }
1061: return l;
1.3 noro 1062: }
1063:
1064: /*
1065: * Executes an SM command.
1066: */
1067:
1.13 noro 1068: void asir_ox_push_cmd(int cmd)
1.3 noro 1069: {
1.7 noro 1070: int ret;
1071: ERR err;
1072: extern char LastError[];
1073:
1.35 noro 1074: if ( ret = SETJMP(main_env) ) {
1.12 noro 1075: asir_reset_handler();
1.7 noro 1076: if ( ret == 1 ) {
1.59 noro 1077: create_error(&err,0,LastError,LastStackTrace); /* XXX */
1.7 noro 1078: asir_push_one((Obj)err);
1079: }
1.12 noro 1080: } else {
1081: asir_save_handler();
1082: asir_set_handler();
1.7 noro 1083: asir_do_cmd(cmd,0);
1.12 noro 1084: asir_reset_handler();
1085: }
1.3 noro 1086: }
1087:
1088: /*
1089: * Executes a string written in Asir.
1090: */
1091:
1092: void asir_ox_execute_string(char *s)
1093: {
1094: STRING str;
1.8 noro 1095: int ret;
1096: ERR err;
1097: extern char LastError[];
1.3 noro 1098:
1099: MKSTR(str,s);
1100: asir_push_one((Obj)str);
1.35 noro 1101: if ( ret = SETJMP(main_env) ) {
1.12 noro 1102: asir_reset_handler();
1.8 noro 1103: if ( ret == 1 ) {
1.59 noro 1104: create_error(&err,0,LastError,LastStackTrace); /* XXX */
1.8 noro 1105: asir_push_one((Obj)err);
1106: }
1.12 noro 1107: } else {
1108: asir_save_handler();
1109: asir_set_handler();
1.8 noro 1110: asir_executeString();
1.12 noro 1111: asir_reset_handler();
1112: }
1.3 noro 1113: }
1114:
1115: /*
1116: * Returns the size as a CMO of the object
1117: * at the top of the stack.
1118: */
1119:
1120: int asir_ox_peek_cmo_size()
1121: {
1122: Obj obj;
1123: int len;
1124:
1.38 noro 1125: obj = asir_peek_one();
1.10 noro 1126: if ( !valid_as_cmo(obj) ) {
1127: fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
1128: return 0;
1129: }
1.3 noro 1130: len = count_as_cmo(obj);
1.38 noro 1131: return len;
1132: }
1133:
1134: int asir_ox_peek_cmo_string_length()
1135: {
1136: Obj obj;
1137: int len;
1138:
1139: obj = asir_peek_one();
1140: if ( !valid_as_cmo(obj) ) {
1141: fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
1142: return 0;
1143: }
1144: len = estimate_length(CO,obj);
1.39 noro 1145: return len+1;
1.3 noro 1146: }
1147:
1148: /*
1149: * Initialization.
1.11 noro 1150: * byteorder=0 => native
1151: * =1 => network byte order
1.3 noro 1152: */
1153:
1.13 noro 1154: int asir_ox_init(int byteorder)
1.3 noro 1155: {
1156: int tmp;
1157: char ifname[BUFSIZ];
1158: extern int GC_dont_gc;
1159: extern int do_asirrc;
1160: extern int do_server_in_X11;
1161: char *getenv();
1162: static ox_asir_initialized = 0;
1163: FILE *ifp;
1164:
1.57 noro 1165: GC_init();
1.43 noro 1166: #if !defined(VISUAL) && !defined(MPI)
1.9 noro 1167: do_server_in_X11 = 0; /* XXX */
1.3 noro 1168: #endif
1169: asir_save_handler();
1.41 ohara 1170: #if defined(PARI)
1.3 noro 1171: risa_pari_init();
1172: #endif
1173: srandom((int)get_current_time());
1174:
1175: rtime_init();
1176: env_init();
1177: endian_init();
1178: /* process_args(argc,argv); */
1179: output_init();
1180: arf_init();
1181: nglob_init();
1182: glob_init();
1183: sig_init();
1184: tty_init();
1185: debug_init();
1186: pf_init();
1187: sysf_init();
1188: parif_init();
1189: #if defined(VISUAL)
1190: init_socket();
1191: #endif
1192: #if defined(UINIT)
1193: reg_sysf();
1194: #endif
1.60 noro 1195: input_init(0,"string");
1.3 noro 1196: sprintf(ifname,"%s/.asirrc",getenv("HOME"));
1197: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
1198: fclose(ifp);
1.60 noro 1199: if ( !SETJMP(main_env) )
1200: execasirfile(ifname);
1.3 noro 1201: }
1202:
1203: asir_OperandStackSize = BUFSIZ;
1204: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
1205: asir_OperandStackPtr = -1;
1.11 noro 1206: if ( little_endian && byteorder )
1207: lib_ox_need_conv = 1;
1.3 noro 1208: else
1.11 noro 1209: lib_ox_need_conv = 0;
1.3 noro 1210: do_message = 0;
1.11 noro 1211: create_my_mathcap("ox_asir");
1.12 noro 1212: asir_reset_handler();
1.13 noro 1213: return 0;
1.1 noro 1214: }
1.17 noro 1215: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>