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