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