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