Annotation of OpenXM_contrib2/asir2000/io/ox_asir.c, Revision 1.63
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.63 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/ox_asir.c,v 1.62 2006/02/25 06:33:31 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.20 noro 600: void asir_popString()
1.1 noro 601: {
602: Obj val;
1.53 noro 603: char *buf;
1.1 noro 604: int l;
605: STRING str;
606:
607: val = asir_pop_one();
608: if ( !val )
1.53 noro 609: buf = "0";
1.1 noro 610: else {
611: l = estimate_length(CO,val);
612: buf = (char *)ALLOCA(l+1);
613: soutput_init(buf);
614: sprintexpr(CO,val);
615: }
1.52 noro 616: MKSTR(str,buf);
1.1 noro 617: ox_send_data(0,str);
618: }
619:
1.20 noro 620: void asir_pops()
1.1 noro 621: {
622: int n;
623:
624: n = (int)(((USINT)asir_pop_one())->body);
625: asir_OperandStackPtr = MAX(asir_OperandStackPtr-n,-1);
626: }
627:
1.20 noro 628: void asir_setName(unsigned int serial)
1.1 noro 629: {
630: char *name;
631: int l,n;
632: char *dummy = "=0;";
633: SNODE snode;
634: ERR err;
635:
636: name = ((STRING)asir_pop_one())->body;
637: l = strlen(name);
638: n = l+strlen(dummy)+1;
639: parse_strp = (char *)ALLOCA(n);
640: sprintf(parse_strp,"%s%s",name,dummy);
641: if ( mainparse(&snode) ) {
1.59 noro 642: create_error(&err,serial,"cannot set to variable",0);
1.1 noro 643: asir_push_one((Obj)err);
644: } else {
645: FA1((FNODE)FA0(snode)) = (pointer)mkfnode(1,I_FORMULA,asir_pop_one());
646: evalstat(snode);
647: }
648: }
649:
1.20 noro 650: void asir_evalName(unsigned int serial)
1.1 noro 651: {
652: char *name;
653: int l,n;
654: SNODE snode;
655: ERR err;
656: pointer val;
657:
658: name = ((STRING)asir_pop_one())->body;
659: l = strlen(name);
660: n = l+2;
661: parse_strp = (char *)ALLOCA(n);
662: sprintf(parse_strp,"%s;",name);
663: if ( mainparse(&snode) ) {
1.59 noro 664: create_error(&err,serial,"no such variable",0);
1.1 noro 665: val = (pointer)err;
666: } else
667: val = evalstat(snode);
668: asir_push_one(val);
669: }
670:
1.55 noro 671: char *augment_backslash(char *s)
672: {
673: char *p,*r;
674: int i;
675:
676: for ( i = 0, p = s; *p; p++, i++ ) if ( *p == '\\' ) i++;
677: r = (char *)MALLOC_ATOMIC((i+1)*sizeof(char));
678: for ( i = 0, p = s; *p; p++, i++ ) {
679: if ( *p == '\\' ) r[i++] = '\\';
680: r[i] = *p;
681: }
682: return r;
683: }
684:
1.20 noro 685: int asir_executeString()
1.1 noro 686: {
687: SNODE snode;
688: pointer val;
689: char *cmd;
1.41 ohara 690: #if defined(PARI)
1.1 noro 691: recover(0);
1.36 noro 692: /* environement is defined in libpari.a */
1.51 saito 693: # if !(PARI_VERSION_CODE > 131588 )
1.36 noro 694: if ( setjmp(environnement) ) {
1.1 noro 695: avma = top; recover(1);
696: resetenv("");
697: }
1.51 saito 698: # endif
1.1 noro 699: #endif
700: cmd = ((STRING)asir_pop_one())->body;
1.58 noro 701: /* XXX : probably this is useless */
702: #if 0
1.55 noro 703: parse_strp = augment_backslash(cmd);
1.58 noro 704: #else
705: parse_strp = cmd;
706: #endif
1.62 noro 707: asir_infile->ln = 1;
1.1 noro 708: if ( mainparse(&snode) ) {
709: return -1;
710: }
711: val = evalstat(snode);
712: if ( NEXT(asir_infile) ) {
713: while ( NEXT(asir_infile) ) {
714: if ( mainparse(&snode) ) {
715: asir_push_one(val);
716: return -1;
717: }
718: nextbp = 0;
719: val = evalstat(snode);
720: }
721: }
722: asir_push_one(val);
723: return 0;
724: }
725:
726: static void asir_executeFunction(int serial)
727: {
728: char *func;
729: int argc;
730: FUNC f;
731: Obj result;
732: NODE n,n1;
733: STRING fname;
734: char *path;
735: ERR err;
1.5 noro 736: Obj arg;
1.1 noro 737: static char buf[BUFSIZ];
738:
1.5 noro 739: arg = asir_pop_one();
740: if ( !arg || OID(arg) != O_STR ) {
741: sprintf(buf,"executeFunction : invalid function name");
742: goto error;
743: } else
744: func = ((STRING)arg)->body;
745:
746: arg = asir_pop_one();
747: if ( !arg || OID(arg) != O_USINT ) {
748: sprintf(buf,"executeFunction : invalid argc");
749: goto error;
750: } else
751: argc = (int)(((USINT)arg)->body);
1.1 noro 752:
753: for ( n = 0; argc; argc-- ) {
754: NEXTNODE(n,n1);
755: BDY(n1) = (pointer)asir_pop_one();
756: }
757: if ( n )
758: NEXT(n1) = 0;
759:
1.59 noro 760: #if 0
1.1 noro 761: if ( !strcmp(func,"load") ) {
762: fname = (STRING)BDY(n);
763: if ( OID(fname) == O_STR ) {
764: searchasirpath(BDY(fname),&path);
765: if ( path ) {
766: if ( do_message )
767: fprintf(stderr,"loading %s\n",path);
768: execasirfile(path);
769: } else
770: if ( do_message )
771: fprintf(stderr,"load : %s not found in the search path\n",BDY(fname));
772: }
773: result = 0;
774: } else {
1.59 noro 775: #endif
1.1 noro 776: searchf(noargsysf,func,&f);
777: if ( !f )
778: searchf(sysf,func,&f);
779: if ( !f )
780: searchf(ubinf,func,&f);
781: if ( !f )
782: searchf(usrf,func,&f);
783: if ( !f ) {
784: sprintf(buf,"executeFunction : the function %s not found",func);
1.5 noro 785: goto error;
1.1 noro 786: } else {
787: result = (Obj)bevalf(f,n);
788: }
1.59 noro 789: #if 0
1.1 noro 790: }
1.59 noro 791: #endif
1.5 noro 792: asir_push_one(result);
793: return;
794:
795: error:
1.59 noro 796: create_error(&err,serial,buf,0);
1.5 noro 797: result = (Obj)err;
1.1 noro 798: asir_push_one(result);
799: }
800:
1.20 noro 801: void asir_end_flush()
1.1 noro 802: {
803: ox_flushing = 0;
804: }
805:
806: /*
807: asir_OperandStackPtr points to the surface of the stack.
808: That is, the data at the stack top is
809: asir_OperandStack[asir_OperandStackPtr].
810: */
811:
812:
1.20 noro 813: void asir_push_one(Obj obj)
1.1 noro 814: {
815: if ( !obj || OID(obj) != O_VOID ) {
816: asir_OperandStackPtr++;
817: if ( asir_OperandStackPtr >= asir_OperandStackSize ) {
818: asir_OperandStackSize += BUFSIZ;
819: asir_OperandStack
820: = (Obj *)REALLOC(asir_OperandStack,
821: asir_OperandStackSize*sizeof(Obj));
822: }
823: asir_OperandStack[asir_OperandStackPtr] = obj;
824: }
825: }
826:
1.20 noro 827: Obj asir_pop_one() {
1.1 noro 828: if ( asir_OperandStackPtr < 0 ) {
829: if ( do_message )
830: fprintf(stderr,"OperandStack underflow");
831: return 0;
832: } else {
833: if ( do_message )
834: fprintf(stderr,"pop at %d\n",asir_OperandStackPtr);
835: return asir_OperandStack[asir_OperandStackPtr--];
1.18 noro 836: }
837: }
838:
1.20 noro 839: Obj asir_peek_one() {
1.18 noro 840: if ( asir_OperandStackPtr < 0 ) {
841: if ( do_message )
842: fprintf(stderr,"OperandStack underflow");
843: return 0;
844: } else {
845: if ( do_message )
846: fprintf(stderr,"peek at %d\n",asir_OperandStackPtr);
847: return asir_OperandStack[asir_OperandStackPtr];
1.1 noro 848: }
849: }
850:
1.40 noro 851: void ox_asir_init(int argc,char **argv,char *servername)
1.1 noro 852: {
853: char ifname[BUFSIZ];
854: extern int GC_dont_gc;
855: extern int do_asirrc;
856: extern int do_server_in_X11;
1.37 noro 857: extern char displayname[];
1.1 noro 858: char *getenv();
859: static ox_asir_initialized = 0;
860: FILE *ifp;
1.4 noro 861: char *homedir;
862: char *ptr;
1.61 noro 863: int do_server_sav;
1.32 noro 864: #if !defined(VISUAL)
865: int tmp;
866: #endif
1.1 noro 867:
1.57 noro 868: GC_init();
1.43 noro 869: #if !defined(VISUAL) && !defined(MPI)
1.1 noro 870: do_server_in_X11 = 1; /* XXX */
871: #endif
872: asir_save_handler();
1.41 ohara 873: #if defined(PARI)
1.1 noro 874: risa_pari_init();
875: #endif
876: srandom((int)get_current_time());
877:
878: rtime_init();
879: env_init();
880: endian_init();
1.44 noro 881: cppname_init();
1.1 noro 882: process_args(--argc,++argv);
1.37 noro 883: #if defined(__CYGWIN__)
884: if ( !displayname[0] )
885: do_server_in_X11 = 0; /* XXX */
886: #endif
1.1 noro 887: output_init();
888: arf_init();
889: nglob_init();
890: glob_init();
891: sig_init();
892: tty_init();
893: debug_init();
894: pf_init();
895: sysf_init();
896: parif_init();
897: #if defined(VISUAL)
898: init_socket();
899: #endif
900: #if defined(UINIT)
901: reg_sysf();
902: #endif
1.4 noro 903: /* if ASIR_CONFIG is set, execute it; else execute .asirrc */
904: if ( ptr = getenv("ASIR_CONFIG") )
905: strcpy(ifname,ptr);
906: else {
907: homedir = getenv("HOME");
908: if ( !homedir ) {
909: char rootname[BUFSIZ];
910:
911: get_rootdir(rootname,sizeof(rootname));
912: homedir = rootname;
913: }
914: sprintf(ifname,"%s/.asirrc",homedir);
915: }
1.59 noro 916:
917: /* the bottom of the input stack */
918: input_init(0,"string");
919:
1.1 noro 920: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
921: fclose(ifp);
1.61 noro 922: do_server_sav = do_server_in_X11;
923: do_server_in_X11 = 0;
1.60 noro 924: if ( !SETJMP(main_env) )
925: execasirfile(ifname);
1.61 noro 926: do_server_in_X11 = do_server_sav;
1.1 noro 927: }
1.59 noro 928:
1.30 noro 929: /* XXX Windows compatibility */
930: ox_io_init();
1.40 noro 931: create_my_mathcap(servername);
1.1 noro 932: }
933:
1.20 noro 934: void ox_io_init() {
1.1 noro 935: unsigned char c,rc;
1.21 noro 936: extern int I_am_server;
1.26 noro 937:
1.33 noro 938: /* XXX : ssh forwards stdin to a remote host on PC Unix */
939: #if defined(linux)
940: #include <sys/param.h>
1.32 noro 941: int i;
942:
943: close(0);
944: for ( i = 5; i < NOFILE; i++ )
945: close(i);
1.33 noro 946: #elif defined(__FreeBSD__)
1.27 noro 947: #include <sys/resource.h>
1.33 noro 948: int i;
1.32 noro 949: struct rlimit rl;
1.27 noro 950:
1.32 noro 951: getrlimit(RLIMIT_NOFILE,&rl);
952: close(0);
953: for ( i = 5; i < rl.rlim_cur; i++ )
954: close(i);
1.27 noro 955: #endif
1.1 noro 956:
1.21 noro 957: I_am_server = 1;
1.1 noro 958: endian_init();
959: #if defined(VISUAL)
960: if ( !ox_sock_id )
961: exit(0);
962: iofp[0].in = WSIO_open(ox_sock_id,"r");
963: iofp[0].out = WSIO_open(ox_sock_id,"w");
964: #else
965: iofp[0].in = fdopen(3,"r");
966: iofp[0].out = fdopen(4,"w");
967:
1.34 noro 968: #if !defined(__CYGWIN__)
1.1 noro 969: setbuffer(iofp[0].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
970: setbuffer(iofp[0].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
1.34 noro 971: #endif
1.1 noro 972: signal(SIGUSR1,ox_usr1_handler);
973: #endif
974: asir_OperandStackSize = BUFSIZ;
975: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
976: asir_OperandStackPtr = -1;
977: if ( little_endian )
978: c = 1;
979: else
980: c = 0xff;
981: /* server : write -> read */
982: write_char(iofp[0].out,&c); ox_flush_stream_force(0);
983: read_char(iofp[0].in,&rc);
984: iofp[0].conv = c == rc ? 0 : 1;
1.14 noro 985: /* XXX; for raw I/O */
986: register_server(0,0,0);
1.3 noro 987: }
988:
1.17 noro 989: #if !defined(VISUAL)
1.3 noro 990: /*
991: * Library mode functions
992: */
993:
994: /*
995: * Converts a binary encoded CMO into a risa object
996: * and pushes it onto the stack.
997: */
998:
999: void asir_ox_push_cmo(void *cmo)
1000: {
1001: Obj obj;
1002:
1003: ox_copy_init(cmo);
1004: ox_buf_to_obj_as_cmo(&obj);
1005: asir_push_one(obj);
1006: }
1007:
1008: /*
1009: * Pop an object from the stack and converts it
1.28 noro 1010: * into a binary encoded CMO.
1.3 noro 1011: */
1012:
1013: int asir_ox_pop_cmo(void *cmo, int limit)
1014: {
1015: Obj obj;
1016: int len;
1.11 noro 1017: ERR err;
1.3 noro 1018:
1019: obj = asir_pop_one();
1.10 noro 1020: if ( !valid_as_cmo(obj) ) {
1.11 noro 1021: asir_push_one(obj);
1.59 noro 1022: create_error(&err,0,"The object at the stack top is invalid as a CMO.",0);
1.11 noro 1023: obj = (Obj)err;
1.10 noro 1024: }
1.3 noro 1025: len = count_as_cmo(obj);
1026: if ( len <= limit ) {
1027: ox_copy_init(cmo);
1028: ox_obj_to_buf_as_cmo(obj);
1029: return len;
1030: } else
1031: return -1;
1.28 noro 1032: }
1033:
1034: int asir_ox_pop_string(void *string, int limit)
1035: {
1036: Obj val;
1037: int l;
1038:
1039: val = asir_pop_one();
1040: if ( !val ) {
1041: if ( limit >= 2 ) {
1042: sprintf(string,"0");
1043: l = strlen(string);
1044: } else
1045: l = -1;
1046: } else {
1047: l = estimate_length(CO,val);
1048: if ( l+1 <= limit ) {
1049: soutput_init(string);
1050: sprintexpr(CO,val);
1051: l = strlen(string);
1052: } else
1053: l = -1;
1054: }
1055: return l;
1.3 noro 1056: }
1057:
1058: /*
1059: * Executes an SM command.
1060: */
1061:
1.13 noro 1062: void asir_ox_push_cmd(int cmd)
1.3 noro 1063: {
1.7 noro 1064: int ret;
1065: ERR err;
1066: extern char LastError[];
1067:
1.35 noro 1068: if ( ret = SETJMP(main_env) ) {
1.12 noro 1069: asir_reset_handler();
1.7 noro 1070: if ( ret == 1 ) {
1.59 noro 1071: create_error(&err,0,LastError,LastStackTrace); /* XXX */
1.7 noro 1072: asir_push_one((Obj)err);
1073: }
1.12 noro 1074: } else {
1075: asir_save_handler();
1076: asir_set_handler();
1.7 noro 1077: asir_do_cmd(cmd,0);
1.12 noro 1078: asir_reset_handler();
1079: }
1.3 noro 1080: }
1081:
1082: /*
1083: * Executes a string written in Asir.
1084: */
1085:
1086: void asir_ox_execute_string(char *s)
1087: {
1088: STRING str;
1.8 noro 1089: int ret;
1090: ERR err;
1091: extern char LastError[];
1.3 noro 1092:
1093: MKSTR(str,s);
1094: asir_push_one((Obj)str);
1.35 noro 1095: if ( ret = SETJMP(main_env) ) {
1.12 noro 1096: asir_reset_handler();
1.8 noro 1097: if ( ret == 1 ) {
1.59 noro 1098: create_error(&err,0,LastError,LastStackTrace); /* XXX */
1.8 noro 1099: asir_push_one((Obj)err);
1100: }
1.12 noro 1101: } else {
1102: asir_save_handler();
1103: asir_set_handler();
1.8 noro 1104: asir_executeString();
1.12 noro 1105: asir_reset_handler();
1106: }
1.3 noro 1107: }
1108:
1109: /*
1110: * Returns the size as a CMO of the object
1111: * at the top of the stack.
1112: */
1113:
1114: int asir_ox_peek_cmo_size()
1115: {
1116: Obj obj;
1117: int len;
1118:
1.38 noro 1119: obj = asir_peek_one();
1.10 noro 1120: if ( !valid_as_cmo(obj) ) {
1121: fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
1122: return 0;
1123: }
1.3 noro 1124: len = count_as_cmo(obj);
1.38 noro 1125: return len;
1126: }
1127:
1128: int asir_ox_peek_cmo_string_length()
1129: {
1130: Obj obj;
1131: int len;
1132:
1133: obj = asir_peek_one();
1134: if ( !valid_as_cmo(obj) ) {
1135: fprintf(stderr,"The object at the stack top is invalid as a CMO.\n");
1136: return 0;
1137: }
1138: len = estimate_length(CO,obj);
1.39 noro 1139: return len+1;
1.3 noro 1140: }
1141:
1142: /*
1143: * Initialization.
1.11 noro 1144: * byteorder=0 => native
1145: * =1 => network byte order
1.3 noro 1146: */
1147:
1.13 noro 1148: int asir_ox_init(int byteorder)
1.3 noro 1149: {
1150: int tmp;
1151: char ifname[BUFSIZ];
1152: extern int GC_dont_gc;
1153: extern int do_asirrc;
1154: extern int do_server_in_X11;
1155: char *getenv();
1156: static ox_asir_initialized = 0;
1157: FILE *ifp;
1158:
1.57 noro 1159: GC_init();
1.43 noro 1160: #if !defined(VISUAL) && !defined(MPI)
1.9 noro 1161: do_server_in_X11 = 0; /* XXX */
1.3 noro 1162: #endif
1163: asir_save_handler();
1.41 ohara 1164: #if defined(PARI)
1.3 noro 1165: risa_pari_init();
1166: #endif
1167: srandom((int)get_current_time());
1168:
1169: rtime_init();
1170: env_init();
1171: endian_init();
1172: /* process_args(argc,argv); */
1173: output_init();
1174: arf_init();
1175: nglob_init();
1176: glob_init();
1177: sig_init();
1178: tty_init();
1179: debug_init();
1180: pf_init();
1181: sysf_init();
1182: parif_init();
1183: #if defined(VISUAL)
1184: init_socket();
1185: #endif
1186: #if defined(UINIT)
1187: reg_sysf();
1188: #endif
1.60 noro 1189: input_init(0,"string");
1.3 noro 1190: sprintf(ifname,"%s/.asirrc",getenv("HOME"));
1191: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
1192: fclose(ifp);
1.60 noro 1193: if ( !SETJMP(main_env) )
1194: execasirfile(ifname);
1.3 noro 1195: }
1196:
1197: asir_OperandStackSize = BUFSIZ;
1198: asir_OperandStack = (Obj *)CALLOC(asir_OperandStackSize,sizeof(Obj));
1199: asir_OperandStackPtr = -1;
1.11 noro 1200: if ( little_endian && byteorder )
1201: lib_ox_need_conv = 1;
1.3 noro 1202: else
1.11 noro 1203: lib_ox_need_conv = 0;
1.3 noro 1204: do_message = 0;
1.11 noro 1205: create_my_mathcap("ox_asir");
1.12 noro 1206: asir_reset_handler();
1.13 noro 1207: return 0;
1.1 noro 1208: }
1.17 noro 1209: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>