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