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