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