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