Annotation of OpenXM_contrib2/asir2000/plot/ox_plot.c, Revision 1.5
1.5 ! 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
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 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.
! 47: *
! 48: * $OpenXM: OpenXM_contrib2/asir2000/plot/ox_plot.c,v 1.4 2000/06/07 08:46:41 noro Exp $
! 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #include "ox.h"
53: #include "ifplot.h"
54: #include "version.h"
1.3 noro 55: #include <signal.h>
1.2 noro 56: #if PARI
57: #include "genpari.h"
58: #endif
1.1 noro 59:
60: void ox_usr1_handler();
61:
62: extern jmp_buf environnement;
63:
64: extern int do_message;
65: extern int ox_flushing;
66: extern jmp_buf ox_env;
67: extern MATHCAP my_mathcap;
68:
69: static int plot_OperandStackSize;
70: static Obj *plot_OperandStack;
71: static int plot_OperandStackPtr = -1;
72:
73: static void create_error(ERR *,unsigned int ,char *);
74: static void process_ox();
75: static void ox_io_init();
76: static void ox_asir_init(int,char **);
77: static Obj asir_pop_one();
78: static void asir_push_one(Obj);
79: static void asir_end_flush();
80: static void asir_executeFunction();
81: static int asir_executeString();
82: static void asir_evalName(unsigned int);
83: static void asir_setName(unsigned int);
84: static void asir_pops();
85: static void asir_popString();
86: static void asir_popCMO(unsigned int);
87: static void asir_popSerializedLocalObject();
88: static char *name_of_cmd(unsigned int);
89: static char *name_of_id(int);
90: static void asir_do_cmd(unsigned int,unsigned int);
1.4 noro 91: static LIST asir_GetErrorList();
1.1 noro 92:
93: static void create_error(ERR *err,unsigned int serial,char *msg)
94: {
95: USINT ui;
96: NODE n,n1;
97: LIST list;
98: STRING errmsg;
99:
100: MKUSINT(ui,serial);
101: MKSTR(errmsg,msg);
102: MKNODE(n1,errmsg,0); MKNODE(n,ui,n1); MKLIST(list,n);
103: MKERR(*err,list);
104: }
105:
106: void ox_plot_main(int argc,char **argv) {
107: int ds;
108: fd_set r;
109: int n;
110:
111: ox_asir_init(argc,argv);
112: init_plot_display(argc,argv);
113: ds = ConnectionNumber(display);
114: if ( do_message )
115: fprintf(stderr,"I'm an ox_plot, Version %d.\n",ASIR_VERSION);
116:
117: if ( setjmp(ox_env) ) {
118: while ( NEXT(asir_infile) )
119: closecurrentinput();
120: reset_current_computation();
121: ox_send_sync(0);
122: }
123: while ( 1 ) {
124: if ( ox_data_is_available(0) )
125: process_ox();
126: else {
127: FD_ZERO(&r);
128: FD_SET(3,&r); FD_SET(ds,&r);
129: select(FD_SETSIZE,&r,NULL,NULL,NULL);
130: if ( FD_ISSET(3,&r) )
131: process_ox();
132: else if ( FD_ISSET(ds,&r) )
133: process_xevent();
134: }
135: }
136: }
137:
138: static void process_ox()
139: {
140: int id;
141: unsigned int cmd;
142: Obj obj;
143: ERR err;
144: unsigned int serial;
145: int ret;
146: extern char LastError[];
147:
148: serial = ox_recv(0,&id,&obj);
149: if ( do_message )
150: fprintf(stderr,"#%d Got %s",serial,name_of_id(id));
151: switch ( id ) {
152: case OX_COMMAND:
153: cmd = ((USINT)obj)->body;
154: if ( ox_flushing )
155: break;
156: if ( do_message )
157: fprintf(stderr," %s\n",name_of_cmd(cmd));
158: if ( ret = setjmp(env) ) {
159: if ( ret == 1 ) {
160: create_error(&err,serial,LastError);
161: asir_push_one((Obj)err);
162: }
163: break;
164: }
165: asir_do_cmd(cmd,serial);
166: break;
167: case OX_DATA:
168: case OX_LOCAL_OBJECT_ASIR:
169: if ( ox_flushing )
170: break;
171: if ( do_message )
172: fprintf(stderr," -> data pushed");
173: asir_push_one(obj);
174: break;
175: case OX_SYNC_BALL:
176: asir_end_flush();
177: break;
178: default:
179: break;
180: }
181: if ( do_message )
182: fprintf(stderr,"\n");
183: }
184:
185: static void asir_do_cmd(unsigned int cmd,unsigned int serial)
186: {
187: MATHCAP client_mathcap;
1.4 noro 188: LIST list;
189: int i;
190: Q q;
1.1 noro 191:
192: switch ( cmd ) {
1.4 noro 193: case SM_dupErrors:
194: list = asir_GetErrorList();
195: asir_push_one((Obj)list);
196: break;
197: case SM_getsp:
198: i = plot_OperandStackPtr+1;
199: STOQ(i,q);
200: asir_push_one((Obj)q);
201: break;
1.1 noro 202: case SM_popSerializedLocalObject:
203: asir_popSerializedLocalObject();
204: break;
205: case SM_popCMO:
206: asir_popCMO(serial);
207: break;
208: case SM_popString:
209: asir_popString();
210: break;
211: case SM_setName:
212: asir_setName(serial);
213: break;
214: case SM_evalName:
215: asir_evalName(serial);
216: break;
217: case SM_executeStringByLocalParser:
218: asir_executeString();
219: break;
220: case SM_executeFunction:
221: asir_executeFunction();
222: break;
223: case SM_shutdown:
224: asir_terminate(2);
225: break;
226: case SM_pops:
227: asir_pops();
228: break;
229: case SM_mathcap:
230: asir_push_one((Obj)my_mathcap);
231: break;
232: case SM_setMathcap:
233: client_mathcap = (MATHCAP)asir_pop_one();
234: store_remote_mathcap(0,client_mathcap);
235: break;
236: default:
237: break;
238: }
239: }
240:
241: static char *name_of_id(int id)
242: {
243: switch ( id ) {
244: case OX_COMMAND:
245: return "OX_COMMAND";
246: break;
247: case OX_DATA:
248: return "OX_DATA";
249: break;
250: case OX_LOCAL_OBJECT_ASIR:
251: return "OX_LOCAL_OBJECT_ASIR";
252: break;
253: case OX_SYNC_BALL:
254: return "OX_SYNC_BALL";
255: break;
256: default:
257: return "Unknown id";
258: break;
259: }
260: }
261:
262: static char *name_of_cmd(unsigned cmd)
263: {
264: switch ( cmd ) {
265: case SM_popSerializedLocalObject:
266: return "SM_popSerializedLocalObject";
267: break;
268: case SM_popCMO:
269: return "SM_popCMO";
270: break;
271: case SM_popString:
272: return "SM_popString";
273: break;
274: case SM_pops:
275: return "SM_pops";
276: break;
277: case SM_setName:
278: return "SM_setName";
279: break;
280: case SM_evalName:
281: return "SM_evalName";
282: break;
283: case SM_executeStringByLocalParser:
284: return "SM_executeString";
285: break;
286: case SM_executeFunction:
287: return "SM_executeFunction";
288: break;
289: case SM_shutdown:
290: return "SM_shutdown";
291: break;
292: case SM_beginBlock:
293: return "SM_beginBlock";
294: break;
295: case SM_endBlock:
296: return "SM_endBlock";
297: break;
298: case SM_mathcap:
299: return "SM_mathcap";
300: break;
301: case SM_setMathcap:
302: return "SM_setMathcap";
303: break;
304: default:
305: return "Unknown cmd";
306: break;
307: }
308: }
309:
310: static void asir_popSerializedLocalObject()
311: {
312: Obj obj;
313: VL t,vl;
314:
315: obj = asir_pop_one();
316: get_vars(obj,&vl);
317: for ( t = vl; t; t = NEXT(t) )
318: if ( t->v->attr == (pointer)V_UC || t->v->attr == (pointer)V_PF )
319: error("bsave : not implemented");
320: ox_send_cmd(0,SM_beginBlock);
321: ox_send_local_ring(0,vl);
322: ox_send_local_data(0,obj);
323: ox_send_cmd(0,SM_endBlock);
324: }
325:
326: static void asir_popCMO(unsigned int serial)
327: {
328: Obj obj;
329: ERR err;
330:
331: obj = asir_pop_one();
332: if ( valid_as_cmo(obj) )
333: ox_send_data(0,obj);
334: else {
335: create_error(&err,serial,"cannot convert to CMO object");
336: ox_send_data(0,err);
337: asir_push_one(obj);
338: }
339: }
340:
341: static void asir_popString()
342: {
343: Obj val;
344: char *buf,*obuf;
345: int l;
346: STRING str;
347:
348: val = asir_pop_one();
349: if ( !val )
350: obuf = 0;
351: else {
352: l = estimate_length(CO,val);
353: buf = (char *)ALLOCA(l+1);
354: soutput_init(buf);
355: sprintexpr(CO,val);
356: l = strlen(buf);
357: obuf = (char *)MALLOC(l+1);
358: strcpy(obuf,buf);
359: }
360: MKSTR(str,obuf);
361: ox_send_data(0,str);
362: }
363:
364: static void asir_pops()
365: {
366: int n;
367:
368: n = (int)(((USINT)asir_pop_one())->body);
369: plot_OperandStackPtr = MAX(plot_OperandStackPtr-n,-1);
370: }
371:
372: static void asir_setName(unsigned int serial)
373: {
374: char *name;
375: int l,n;
376: char *dummy = "=0;";
377: SNODE snode;
378: ERR err;
379:
380: name = ((STRING)asir_pop_one())->body;
381: l = strlen(name);
382: n = l+strlen(dummy)+1;
383: parse_strp = (char *)ALLOCA(n);
384: sprintf(parse_strp,"%s%s",name,dummy);
385: if ( mainparse(&snode) ) {
386: create_error(&err,serial,"cannot set to variable");
387: asir_push_one((Obj)err);
388: } else {
389: FA1((FNODE)FA0(snode)) = (pointer)mkfnode(1,I_FORMULA,asir_pop_one());
390: evalstat(snode);
391: }
392: }
393:
394: static void asir_evalName(unsigned int serial)
395: {
396: char *name;
397: int l,n;
398: SNODE snode;
399: ERR err;
400: pointer val;
401:
402: name = ((STRING)asir_pop_one())->body;
403: l = strlen(name);
404: n = l+2;
405: parse_strp = (char *)ALLOCA(n);
406: sprintf(parse_strp,"%s;",name);
407: if ( mainparse(&snode) ) {
408: create_error(&err,serial,"no such variable");
409: val = (pointer)err;
410: } else
411: val = evalstat(snode);
412: asir_push_one(val);
413: }
414:
415: static int asir_executeString()
416: {
417: SNODE snode;
418: pointer val;
419: char *cmd;
420: #if PARI
421: recover(0);
422: if ( setjmp(environnement) ) {
423: avma = top; recover(1);
424: resetenv("");
425: }
426: #endif
427: cmd = ((STRING)asir_pop_one())->body;
428: parse_strp = cmd;
429: if ( mainparse(&snode) ) {
430: return -1;
431: }
432: val = evalstat(snode);
433: if ( NEXT(asir_infile) ) {
434: while ( NEXT(asir_infile) ) {
435: if ( mainparse(&snode) ) {
436: asir_push_one(val);
437: return -1;
438: }
439: nextbp = 0;
440: val = evalstat(snode);
441: }
442: }
443: asir_push_one(val);
444: return 0;
445: }
446:
447: static void asir_executeFunction()
448: {
449: char *func;
450: int argc;
451: int id;
452: FUNC f;
453: Q ret;
454: VL vl;
455: NODE n,n1;
456:
457: func = ((STRING)asir_pop_one())->body;
458: argc = (int)(((USINT)asir_pop_one())->body);
459:
460: for ( n = 0; argc; argc-- ) {
461: NEXTNODE(n,n1);
462: BDY(n1) = (pointer)asir_pop_one();
463: }
464: if ( n )
465: NEXT(n1) = 0;
466: id = -1;
467: if ( !strcmp(func,"plot") )
468: id = plot(n);
469: else if ( !strcmp(func,"arrayplot") )
470: id = arrayplot(n);
471: else if ( !strcmp(func,"plotover") )
472: id = plotover(n);
473: else if ( !strcmp(func,"drawcircle") )
474: id = drawcircle(n);
475: STOQ(id,ret);
476: #if 0
477: asir_push_one((Obj)ret);
478: #endif
479: }
480:
481: static void asir_end_flush()
482: {
483: ox_flushing = 0;
484: }
485:
486: static void asir_push_one(Obj obj)
487: {
488: if ( !obj || OID(obj) != O_VOID ) {
489: plot_OperandStackPtr++;
490: if ( plot_OperandStackPtr >= plot_OperandStackSize ) {
491: plot_OperandStackSize += BUFSIZ;
492: plot_OperandStack
493: = (Obj *)REALLOC(plot_OperandStack,
494: plot_OperandStackSize*sizeof(Obj));
495: }
496: plot_OperandStack[plot_OperandStackPtr] = obj;
497: }
1.4 noro 498: }
499:
500: static LIST asir_GetErrorList()
501: {
502: int i;
503: NODE n,n0;
504: LIST err;
505: Obj obj;
506:
507: for ( i = 0, n0 = 0; i <= plot_OperandStackPtr; i++ )
508: if ( (obj = plot_OperandStack[i]) && (OID(obj) == O_ERR) ) {
509: NEXTNODE(n0,n); BDY(n) = (pointer)obj;
510: }
511: if ( n0 )
512: NEXT(n) = 0;
513: MKLIST(err,n0);
514: return err;
1.1 noro 515: }
516:
517: static Obj asir_pop_one() {
518: if ( plot_OperandStackPtr < 0 ) {
519: if ( do_message )
520: fprintf(stderr,"OperandStack underflow");
521: return 0;
522: } else {
523: if ( do_message )
524: fprintf(stderr,"pop at %d\n",plot_OperandStackPtr);
525: return plot_OperandStack[plot_OperandStackPtr--];
526: }
527: }
528:
529: static void ox_asir_init(int argc,char **argv)
530: {
531: int tmp;
532: char ifname[BUFSIZ];
533: extern int GC_dont_gc;
534: extern int read_exec_file;
535: extern int do_asirrc;
536: extern int do_server_in_X11;
537: char *getenv();
538: static ox_asir_initialized = 0;
539: FILE *ifp;
540:
541: do_server_in_X11 = 1; /* XXX */
542: asir_save_handler();
543: #if PARI
544: risa_pari_init();
545: #endif
546: srandom((int)get_current_time());
547:
548: #if defined(THINK_C)
549: param_init();
550: #endif
551: StackBottom = &tmp + 1; /* XXX */
552: rtime_init();
553: env_init();
554: endian_init();
555: #if !defined(VISUAL) && !defined(THINK_C)
556: /* check_key(); */
557: #endif
558: GC_init();
559: process_args(--argc,++argv);
560: #if 0
561: copyright();
562: #endif
563: output_init();
564: arf_init();
565: nglob_init();
566: glob_init();
567: sig_init();
568: tty_init();
569: debug_init();
570: pf_init();
571: sysf_init();
572: parif_init();
573: #if defined(VISUAL)
574: init_socket();
575: #endif
576: #if defined(UINIT)
577: reg_sysf();
578: #endif
579: #if defined(THINK_C)
580: sprintf(ifname,"asirrc");
581: #else
582: sprintf(ifname,"%s/.asirrc",getenv("HOME"));
583: #endif
584: if ( do_asirrc && (ifp = fopen(ifname,"r")) ) {
585: input_init(ifp,ifname);
586: if ( !setjmp(env) ) {
587: read_exec_file = 1;
588: read_eval_loop();
589: read_exec_file = 0;
590: }
591: fclose(ifp);
592: }
593: input_init(0,"string");
594: ox_io_init();
595: create_my_mathcap("ox_plot");
596: }
597:
598: static void ox_io_init() {
599: unsigned char c,rc;
600: extern int little_endian;
601:
602: endian_init();
603: iofp[0].in = fdopen(3,"r");
604: iofp[0].out = fdopen(4,"w");
605: setbuffer(iofp[0].in,(char *)malloc(LBUFSIZ),LBUFSIZ);
606: setbuffer(iofp[0].out,(char *)malloc(LBUFSIZ),LBUFSIZ);
607: plot_OperandStackSize = BUFSIZ;
608: plot_OperandStack = (Obj *)CALLOC(plot_OperandStackSize,sizeof(Obj));
609: plot_OperandStackPtr = -1;
610: signal(SIGUSR1,ox_usr1_handler);
611: if ( little_endian )
612: c = 1;
613: else
614: c = 0xff;
615: write_char(iofp[0].out,&c); ox_flush_stream(0);
616: read_char(iofp[0].in,&rc);
617: iofp[0].conv = c == rc ? 0 : 1;
618: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>