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