Annotation of OpenXM_contrib2/asir2000/builtin/miscf.c, Revision 1.18
1.7 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.8 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.7 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.18 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/miscf.c,v 1.17 2003/04/02 06:48:19 noro Exp $
1.7 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
1.17 noro 52: #if !defined(VISUAL) && defined(DO_PLOT)
1.1 noro 53: #include <X11/Xlib.h>
54: #include <X11/cursorfont.h>
55: #endif
56:
1.12 noro 57: #if defined(VISUAL)
58: #include <stdlib.h>
59: #include <windows.h>
60: #endif
61:
1.1 noro 62: void Pquit(), Pdebug(), Pnmono(), Pnez(), Popt(), Pshell(), Pheap();
63: void Perror(), Perror3(), Pversion(), Pflist(), Pdelete_history(), Ppause(), Pxpause();
64: void Pr2g(), Pread_cmo(), Pwrite_cmo();
65: void Pgc(),Pbatch(),Psend_progress();
66: void Pnull_command();
67: void Pgetenv();
1.9 noro 68: void Pget_addr(),Phex_dump();
69: void Ppeek(),Ppoke();
1.12 noro 70: void Psleep();
1.18 ! noro 71: void Premove_module();
! 72: void Pmodule_list();
1.1 noro 73:
74: void delete_history(int,int);
75:
76: struct ftab misc_tab[] = {
1.18 ! noro 77: {"module_list",Pmodule_list,0},
! 78: {"remove_module",Premove_module,1},
1.12 noro 79: {"sleep",Psleep,1},
1.1 noro 80: {"null_command",Pnull_command,-99999},
81: {"getenv",Pgetenv,1},
82: {"end",Pquit,0},
83: {"quit",Pquit,0},
84: {"debug",Pdebug,0},
85: {"shell",Pshell,-2},
86: {"heap",Pheap,-1},
1.11 noro 87: {"version",Pversion,-99999},
1.1 noro 88: {"nmono",Pnmono,1},
89: {"error",Perror,1},
90: {"error3",Perror3,3},
91: {"nez",Pnez,1},
1.18 ! noro 92: {"flist",Pflist,-1},
1.1 noro 93: {"delete_history",Pdelete_history,-2},
94: {"pause",Ppause,0},
95: {"gc",Pgc,0},
96: {"batch",Pbatch,2},
97: {"send_progress",Psend_progress,-2},
1.9 noro 98: {"get_addr",Pget_addr,1},
99: {"hex_dump",Phex_dump,2},
100: {"peek",Ppeek,1},
101: {"poke",Ppoke,2},
1.16 noro 102: #if !defined(VISUAL) && defined(DO_PLOT)
1.1 noro 103: {"xpause",Pxpause,0},
104: #endif
105: #if 0
106: {"opt",Popt,1},
107: #endif
108: {0,0,0},
109: };
1.12 noro 110:
111: void Psleep(arg,rp)
112: NODE arg;
113: Q *rp;
114: {
115: int ms;
116:
117: ms = QTOS((Q)ARG0(arg));
118: #if defined(VISUAL)
119: Sleep(ms);
120: #else
121: usleep(ms*1000);
122: #endif
123: *rp = ONE;
124: }
1.1 noro 125:
1.18 ! noro 126: void Pmodule_list(rp)
! 127: LIST *rp;
! 128: {
! 129: char *name;
! 130: NODE r,r1,m;
! 131: STRING s;
! 132:
! 133: r = 0;
! 134: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
! 135: MKSTR(s,((MODULE)BDY(m))->name);
! 136: MKNODE(r1,s,r); r = r1;
! 137: }
! 138: MKLIST(*rp,r);
! 139: }
! 140:
! 141: void Premove_module(arg,rp)
! 142: NODE arg;
! 143: Q *rp;
! 144: {
! 145: NODE pm,m;
! 146: char *name;
! 147:
! 148: asir_assert(ARG0(arg),O_STR,"remove_module");
! 149: name = BDY((STRING)ARG0(arg));
! 150: for ( pm = 0, m = MODULE_LIST; m; pm = m, m = NEXT(m) )
! 151: if ( !strcmp(name,((MODULE)BDY(m))->name) ) {
! 152: if ( !pm )
! 153: MODULE_LIST = NEXT(MODULE_LIST);
! 154: else
! 155: NEXT(pm) = NEXT(m);
! 156: *rp = ONE;
! 157: return;
! 158: }
! 159: *rp = 0;
! 160: }
! 161:
1.1 noro 162: void Pgetenv(arg,rp)
163: NODE arg;
164: STRING *rp;
165: {
166: char *e,*f;
167: int len;
168:
169: e = (char *)getenv(BDY((STRING)ARG0(arg)));
170: if ( e ) {
171: len = strlen(e);
172: f = (char *)MALLOC_ATOMIC(len+1);
173: strcpy(f,e);
174: MKSTR(*rp,f);
175: } else
176: *rp = 0;
177: }
178:
179: void Pnull_command(arg,rp)
180: NODE arg;
181: Q *rp;
182: {
183: *rp = 0;
184: }
185:
186: void Pquit(rp)
187: pointer *rp;
188: {
189: if ( !NEXT(asir_infile) )
190: asir_terminate(2);
191: else {
192: closecurrentinput();
1.5 noro 193: if ( !asir_infile->fp && strcmp(asir_infile->name,"string") )
1.1 noro 194: asir_terminate(2);
195: }
196: *rp = 0;
197: }
198:
199: void Pdebug(rp)
200: pointer *rp;
201: {
202: debug(0); *rp = 0;
203: }
204:
205: void Pshell(arg,rp)
206: NODE arg;
207: Q *rp;
208: {
209: char *com = 0;
210: char *pstr = 0;
211: int status;
212:
213: if ( arg ) {
214: asir_assert(ARG0(arg),O_STR,"shell");
215: com = BDY((STRING)ARG0(arg));
216: if ( NEXT(arg) )
217: pstr = BDY((STRING)ARG1(arg));
218: }
219: status = system(com);
220: STOQ(status,*rp);
221: }
222:
223: void Pnmono(arg,rp)
224: NODE arg;
225: Q *rp;
226: {
227: Obj obj;
228: int n;
229:
230: obj = (Obj)ARG0(arg);
231: if ( !obj || OID(obj) > O_R )
232: *rp = 0;
233: else
234: switch (OID(obj)) {
235: case O_N: case O_P:
236: n = nmonop((P)obj); STOQ(n,*rp); break;
237: case O_R:
238: n = nmonop(NM((R)obj)) + nmonop(DN((R)obj));
239: STOQ(n,*rp); break;
240: }
241: }
242:
243: void Pheap(arg,rp)
244: NODE arg;
245: Q *rp;
246: {
247: int h0,h;
248: void GC_expand_hp(int);
249:
250: h0 = get_heapsize();
251: if ( arg ) {
252: h = QTOS((Q)ARG0(arg));
253: if ( h > h0 )
254: GC_expand_hp(h-h0);
255: }
256: h = get_heapsize();
257: STOQ(h,*rp);
258: }
259:
260: unsigned int get_asir_version();
1.11 noro 261: char *get_asir_distribution();
1.1 noro 262:
1.11 noro 263: void Pversion(arg,rp)
264: NODE arg;
265: Obj *rp;
1.1 noro 266: {
267: unsigned int version;
1.11 noro 268: char *distribution;
269: Q q;
270: STRING str;
271: NODE n;
272: LIST l;
1.1 noro 273:
274: version = get_asir_version();
1.11 noro 275: distribution = get_asir_distribution();
1.13 noro 276: UTOQ(version,q);
1.11 noro 277: if ( !argc(arg) )
278: *rp = (Obj)q;
279: else {
280: MKSTR(str,distribution);
281: n = mknode(2,q,str);
282: MKLIST(l,n);
283: *rp = (Obj)l;
284: }
1.1 noro 285: }
286:
287: extern int nez;
288:
289: void Pnez(arg,rp)
290: NODE arg;
291: pointer *rp;
292: {
293: nez = ARG0(arg) ? 1 : 0; *rp = 0;
294: }
295:
296: void Perror(arg,rp)
297: NODE arg;
298: Q *rp;
299: {
300: char *s;
301:
302: if ( !arg || !ARG0(arg) || (OID((Obj)ARG0(arg)) != O_STR) )
303: s = "";
304: else
305: s = BDY((STRING)ARG0(arg));
306: error(s);
307: *rp = 0;
308: }
309:
310: void Perror3(arg,rp)
311: NODE arg;
312: Q *rp;
313: {
314: int code;
315: char *reason,*action;
316:
317: asir_assert(ARG0(arg),O_N,"error3");
318: asir_assert(ARG1(arg),O_STR,"error3");
319: asir_assert(ARG2(arg),O_STR,"error3");
320: code = QTOS((Q)ARG0(arg));
321: reason = BDY((STRING)ARG1(arg));
322: action = BDY((STRING)ARG2(arg));
1.3 noro 323: #if defined(VISUAL)
1.1 noro 324: set_error(code,reason,action);
1.3 noro 325: #endif
1.1 noro 326: error("");
327: *rp = 0;
328: }
329:
1.18 ! noro 330: void Pflist(arg,rp)
! 331: NODE arg;
1.1 noro 332: LIST *rp;
333: {
334: char *n;
335: STRING name;
1.18 ! noro 336: char *mname;
! 337: NODE t,r,r0,m;
1.1 noro 338: LIST l;
339:
1.18 ! noro 340: if ( argc(arg) ) {
! 341: /* module name is specified */
! 342: asir_assert(ARG0(arg),O_STR,"flist");
! 343: mname = BDY((STRING)ARG0(arg));
! 344: r0 = 0;
! 345: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
! 346: if ( !strcmp(mname,((MODULE)BDY(m))->name) ) {
! 347: t = ((MODULE)BDY(m))->usrf_list;
! 348: for ( r0 = 0; t; t = NEXT(t) )
! 349: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
! 350: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
! 351: MKNODE(r,name,r0); r0 = r;
! 352: }
! 353: }
1.1 noro 354: }
1.18 ! noro 355: } else {
! 356: for ( t = usrf, r0 = 0; t; t = NEXT(t) )
! 357: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
! 358: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
! 359: MKNODE(r,name,r0); r0 = r;
! 360: }
! 361: for ( t = ubinf; t; t = NEXT(t) )
! 362: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
! 363: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
! 364: MKNODE(r,name,r0); r0 = r;
! 365: }
! 366: for ( t = sysf; t; t = NEXT(t) )
! 367: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
! 368: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
! 369: MKNODE(r,name,r0); r0 = r;
! 370: }
! 371: }
1.1 noro 372: MKLIST(l,r0); *rp = l;
373: }
374:
375: void Pdelete_history(arg,rp)
376: NODE arg;
377: Q *rp;
378: {
379: switch ( argc(arg) ) {
380: case 0: default:
381: delete_history(0,(int)APVS->n);
382: break;
383: case 1:
384: delete_history(QTOS((Q)ARG0(arg)),1);
385: break;
386: }
387: *rp = 0;
388: }
389:
390: void delete_history(start,n)
391: int start,n;
392: {
393: int i,max;
394:
395: max = APVS->n;
396: if ( start < 0 || start >= max )
397: return;
398: if ( start + n > max )
399: n = max - start;
400: for ( i = 0; i < n; i++ )
401: APVS->va[start+i].priv = 0;
402: }
403:
404: void Ppause(rp)
405: LIST *rp;
406: {
407: char buf[BUFSIZ];
408:
409: fgets(buf,BUFSIZ,stdin);
410: *rp = 0;
411: }
412:
413: void Pgc(rp)
414: LIST *rp;
415: {
416: GC_gcollect();
417: *rp = 0;
418: }
419:
420: int exec_file(char *,char *);
421:
422: void Pbatch(arg,rp)
423: NODE arg;
424: Q *rp;
425: {
426: int ret;
427:
428: ret = exec_file(BDY((STRING)ARG0(arg)),BDY((STRING)ARG1(arg)));
429: STOQ(ret,*rp);
430: }
431:
1.16 noro 432: #if !defined(VISUAL) && defined(DO_PLOT)
1.1 noro 433: void Pxpause(rp)
434: Q *rp;
435: {
436: if ( !init_display() )
437: *rp = 0;
438: else {
439: grab_pointer(); *rp = ONE;
440: }
441: }
442:
443: static Display *display;
444: static Window rootwin;
445:
446: init_display()
447: {
448: char *dname;
449: unsigned int tmp;
450: static int initialized;
451: int argc;
452: char *argv[1];
453:
454: if ( initialized )
455: return 1;
456: else
457: initialized = 1;
458: dname = (char *)getenv("DISPLAY");
459:
460: display = XOpenDisplay(dname);
461: if ( !display ) {
462: fprintf(stderr,"Can't open display\n");
463: return 0;
464: }
465: rootwin = RootWindow(display,DefaultScreen(display));
466: }
467:
468: grab_pointer()
469: {
470: XEvent ev;
471: static Cursor cursor;
472:
473: if ( !cursor )
474: cursor = XCreateFontCursor(display,XC_leftbutton);
475: XGrabPointer(display,rootwin,True,ButtonPressMask,GrabModeAsync,GrabModeAsync,None,cursor,CurrentTime);
476: while ( 1 ) {
477: XNextEvent(display,&ev);
478: if ( ev.xany.type == ButtonPress )
479: break;
480: }
481: XUngrabPointer(display,CurrentTime);
482: XSync(display,False);
483: return;
484: }
485: #endif
486:
487: void Psend_progress(NODE arg,Q *rp)
488: {
489: #if defined(VISUAL)
490: short per;
491: char *msg;
492:
493: per = (short)QTOS((Q)BDY(arg)); arg = NEXT(arg);
494: if ( arg )
495: msg = BDY((STRING)BDY(arg));
496: else
497: msg = "";
498: send_progress(per,msg);
499: #endif
1.9 noro 500: *rp = 0;
501: }
502:
503: void Pget_addr(arg,rp)
504: NODE arg;
505: Q *rp;
506: {
507: pointer obj;
508: unsigned int u,l;
509: N n;
510:
511: obj = ARG0(arg);
512: if ( sizeof(pointer) == sizeof(unsigned int) ) {
513: UTOQ((unsigned int)obj,*rp);
514: } else {
515: /* a pointer must fit in long */
516: u = ((unsigned long)obj)>>32;
517: l = ((unsigned long)obj)&(unsigned long)0xffffffff;
518: if ( u ) {
519: n = NALLOC(2); PL(n) = 2; BD(n)[0] = l; BD(n)[1] = u;
520: NTOQ(n,1,*rp);
521: } else {
522: UTOQ(l,*rp);
523: }
524: }
525: }
526:
527: unsigned char *qtoaddr(q)
528: Q q;
529: {
530: unsigned char *addr;
531: N n;
532:
533: if ( !q )
534: return 0;
535: n = NM(q);
536: if ( (sizeof(pointer) == sizeof(unsigned int)) || (PL(n) == 1) )
537: addr = (char *)BD(n)[0];
538: else {
539: /* a pointer must fit in long */
540: addr = (char *)((((unsigned long)BD(n)[1])<<32)
541: | ((unsigned long)BD(n)[0]));
542: }
543: return addr;
544: }
545:
546: void Phex_dump(arg,rp)
547: NODE arg;
548: Q *rp;
549: {
550: unsigned char *start;
551: int len,i;
552:
553: *rp = 0;
554: start = qtoaddr((Q)ARG0(arg));
555: len = QTOS((Q)ARG1(arg));
556: for ( i = 0; i < len; i++ ) {
557: if ( !(i%16) )
558: fprintf(asir_out,"%08x: ",start+i);
559: fprintf(asir_out,"%02x",start[i]);
560: if ( !((i+1)%16) )
561: fprintf(asir_out,"\n");
562: else if ( !((i+1)%4) )
563: fprintf(asir_out," ");
564: }
565: if ( i%16 )
566: fprintf(asir_out,"\n");
567: }
568:
569: void Ppeek(arg,rp)
570: NODE arg;
571: Q *rp;
572: {
573: unsigned int b;
574: unsigned char *a;
575:
576: a = qtoaddr((Q)ARG0(arg));
577: b = (unsigned int) (*a);
578: UTOQ(b,*rp);
579: }
580:
581: void Ppoke(arg,rp)
582: NODE arg;
583: Q *rp;
584: {
585: unsigned char *addr;
586:
587: addr = qtoaddr((Q)ARG0(arg));
588: *addr = (unsigned char)QTOS((Q)ARG1(arg));
1.1 noro 589: *rp = 0;
590: }
591:
592: #if 0
593: static int optimize;
594: static struct oN oPSN[1000];
595: static struct oQ oPSZ[1000],oMSZ[1000];
596: static szinit = 0;
597:
598: void Popt(arg,rp)
599: NODE arg;
600: pointer *rp;
601: {
602: optimize = ARG0(arg) ? 1 : 0; *rp = 0;
603: }
604:
605:
606: void sz_init() {
607: int i;
608: Q t;
609:
610: for ( i = 1; i < 1000; i++ ) {
611: oPSN[i].p = 1; oPSN[i].b[0] = i;
612: t = &oPSZ[i];
613: OID(t) = O_N; NID(t) = N_Q; SGN(t) = 1; NM(t) = &oPSN[i]; DN(t) = 0;
614: t = &oMSZ[i];
615: OID(t) = O_N; NID(t) = N_Q; SGN(t) = -1; NM(t) = &oPSN[i]; DN(t) = 0;
616: }
617: szinit = 1;
618: }
619:
620: optobj(p)
621: Obj *p;
622: {
623: Obj t;
624: int n;
625: DCP dc;
626:
627: if ( t = *p )
628: switch ( OID(t) ) {
629: case O_N:
630: if ( (NID(t)==N_Q) && INT(t) && (PL(NM((Q)t))==1) ) {
631: n = QTOS((Q)t);
632: if ( !szinit )
633: sz_init();
634: if ( n < 1000 )
635: *p = (Obj)(SGN((Q)t)>0?&oPSZ[n]:&oMSZ[n]);
636: }
637: break;
638: case O_P:
639: for ( dc = DC((P)t); dc; dc = NEXT(dc) ) {
640: optobj(&DEG(dc)); optobj(&COEF(dc));
641: }
642: break;
643: case O_R:
644: optobj(&NM((R)t)); optobj(&DN((R)t)); break;
645: default:
646: break;
647: }
648: }
649: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>