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