Annotation of OpenXM_contrib2/asir2000/builtin/miscf.c, Revision 1.12
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.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/miscf.c,v 1.11 2001/03/08 07:49:11 noro Exp $
1.7 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
1.10 noro 52: #if !defined(VISUAL)
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.10 noro 98: #if !defined(VISUAL) && 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();
236: STOQ(version,q);
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: char s[BUFSIZ];
275: int code;
276: char *reason,*action;
277:
278: asir_assert(ARG0(arg),O_N,"error3");
279: asir_assert(ARG1(arg),O_STR,"error3");
280: asir_assert(ARG2(arg),O_STR,"error3");
281: code = QTOS((Q)ARG0(arg));
282: reason = BDY((STRING)ARG1(arg));
283: action = BDY((STRING)ARG2(arg));
1.3 noro 284: #if defined(VISUAL)
1.1 noro 285: set_error(code,reason,action);
1.3 noro 286: #endif
1.1 noro 287: error("");
288: *rp = 0;
289: }
290:
291: void Pflist(rp)
292: LIST *rp;
293: {
294: char *n;
295: STRING name;
296: NODE t,r,r0;
297: LIST l;
298:
299: for ( t = usrf, r0 = 0; t; t = NEXT(t) )
300: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
301: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
302: MKNODE(r,name,r0); r0 = r;
303: }
304: for ( t = ubinf; t; t = NEXT(t) )
305: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
306: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
307: MKNODE(r,name,r0); r0 = r;
308: }
309: for ( t = sysf; t; t = NEXT(t) )
310: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
311: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
312: MKNODE(r,name,r0); r0 = r;
313: }
314: MKLIST(l,r0); *rp = l;
315: }
316:
317: void Pdelete_history(arg,rp)
318: NODE arg;
319: Q *rp;
320: {
321: switch ( argc(arg) ) {
322: case 0: default:
323: delete_history(0,(int)APVS->n);
324: break;
325: case 1:
326: delete_history(QTOS((Q)ARG0(arg)),1);
327: break;
328: }
329: *rp = 0;
330: }
331:
332: void delete_history(start,n)
333: int start,n;
334: {
335: int i,max;
336:
337: max = APVS->n;
338: if ( start < 0 || start >= max )
339: return;
340: if ( start + n > max )
341: n = max - start;
342: for ( i = 0; i < n; i++ )
343: APVS->va[start+i].priv = 0;
344: }
345:
346: void Ppause(rp)
347: LIST *rp;
348: {
349: char buf[BUFSIZ];
350:
351: fgets(buf,BUFSIZ,stdin);
352: *rp = 0;
353: }
354:
355: void Pgc(rp)
356: LIST *rp;
357: {
358: GC_gcollect();
359: *rp = 0;
360: }
361:
362: int exec_file(char *,char *);
363:
364: void Pbatch(arg,rp)
365: NODE arg;
366: Q *rp;
367: {
368: int ret;
369:
370: ret = exec_file(BDY((STRING)ARG0(arg)),BDY((STRING)ARG1(arg)));
371: STOQ(ret,*rp);
372: }
373:
1.10 noro 374: #if !defined(VISUAL) && DO_PLOT
1.1 noro 375: void Pxpause(rp)
376: Q *rp;
377: {
378: if ( !init_display() )
379: *rp = 0;
380: else {
381: grab_pointer(); *rp = ONE;
382: }
383: }
384:
385: static Display *display;
386: static Window rootwin;
387:
388: init_display()
389: {
390: char *dname;
391: unsigned int tmp;
392: static int initialized;
393: int argc;
394: char *argv[1];
395:
396: if ( initialized )
397: return 1;
398: else
399: initialized = 1;
400: dname = (char *)getenv("DISPLAY");
401:
402: display = XOpenDisplay(dname);
403: if ( !display ) {
404: fprintf(stderr,"Can't open display\n");
405: return 0;
406: }
407: rootwin = RootWindow(display,DefaultScreen(display));
408: }
409:
410: grab_pointer()
411: {
412: XEvent ev;
413: static Cursor cursor;
414:
415: if ( !cursor )
416: cursor = XCreateFontCursor(display,XC_leftbutton);
417: XGrabPointer(display,rootwin,True,ButtonPressMask,GrabModeAsync,GrabModeAsync,None,cursor,CurrentTime);
418: while ( 1 ) {
419: XNextEvent(display,&ev);
420: if ( ev.xany.type == ButtonPress )
421: break;
422: }
423: XUngrabPointer(display,CurrentTime);
424: XSync(display,False);
425: return;
426: }
427: #endif
428:
429: void Psend_progress(NODE arg,Q *rp)
430: {
431: #if defined(VISUAL)
432: short per;
433: char *msg;
434:
435: per = (short)QTOS((Q)BDY(arg)); arg = NEXT(arg);
436: if ( arg )
437: msg = BDY((STRING)BDY(arg));
438: else
439: msg = "";
440: send_progress(per,msg);
441: #endif
1.9 noro 442: *rp = 0;
443: }
444:
445: void Pget_addr(arg,rp)
446: NODE arg;
447: Q *rp;
448: {
449: pointer obj;
450: unsigned int u,l;
451: N n;
452:
453: obj = ARG0(arg);
454: if ( sizeof(pointer) == sizeof(unsigned int) ) {
455: UTOQ((unsigned int)obj,*rp);
456: } else {
457: /* a pointer must fit in long */
458: u = ((unsigned long)obj)>>32;
459: l = ((unsigned long)obj)&(unsigned long)0xffffffff;
460: if ( u ) {
461: n = NALLOC(2); PL(n) = 2; BD(n)[0] = l; BD(n)[1] = u;
462: NTOQ(n,1,*rp);
463: } else {
464: UTOQ(l,*rp);
465: }
466: }
467: }
468:
469: unsigned char *qtoaddr(q)
470: Q q;
471: {
472: unsigned char *addr;
473: N n;
474:
475: if ( !q )
476: return 0;
477: n = NM(q);
478: if ( (sizeof(pointer) == sizeof(unsigned int)) || (PL(n) == 1) )
479: addr = (char *)BD(n)[0];
480: else {
481: /* a pointer must fit in long */
482: addr = (char *)((((unsigned long)BD(n)[1])<<32)
483: | ((unsigned long)BD(n)[0]));
484: }
485: return addr;
486: }
487:
488: void Phex_dump(arg,rp)
489: NODE arg;
490: Q *rp;
491: {
492: unsigned char *start;
493: int len,i;
494:
495: *rp = 0;
496: start = qtoaddr((Q)ARG0(arg));
497: len = QTOS((Q)ARG1(arg));
498: for ( i = 0; i < len; i++ ) {
499: if ( !(i%16) )
500: fprintf(asir_out,"%08x: ",start+i);
501: fprintf(asir_out,"%02x",start[i]);
502: if ( !((i+1)%16) )
503: fprintf(asir_out,"\n");
504: else if ( !((i+1)%4) )
505: fprintf(asir_out," ");
506: }
507: if ( i%16 )
508: fprintf(asir_out,"\n");
509: }
510:
511: void Ppeek(arg,rp)
512: NODE arg;
513: Q *rp;
514: {
515: unsigned int b;
516: unsigned char *a;
517:
518: a = qtoaddr((Q)ARG0(arg));
519: b = (unsigned int) (*a);
520: UTOQ(b,*rp);
521: }
522:
523: void Ppoke(arg,rp)
524: NODE arg;
525: Q *rp;
526: {
527: unsigned char *addr;
528:
529: addr = qtoaddr((Q)ARG0(arg));
530: *addr = (unsigned char)QTOS((Q)ARG1(arg));
1.1 noro 531: *rp = 0;
532: }
533:
534: #if 0
535: static int optimize;
536: static struct oN oPSN[1000];
537: static struct oQ oPSZ[1000],oMSZ[1000];
538: static szinit = 0;
539:
540: void Popt(arg,rp)
541: NODE arg;
542: pointer *rp;
543: {
544: optimize = ARG0(arg) ? 1 : 0; *rp = 0;
545: }
546:
547:
548: void sz_init() {
549: int i;
550: Q t;
551:
552: for ( i = 1; i < 1000; i++ ) {
553: oPSN[i].p = 1; oPSN[i].b[0] = i;
554: t = &oPSZ[i];
555: OID(t) = O_N; NID(t) = N_Q; SGN(t) = 1; NM(t) = &oPSN[i]; DN(t) = 0;
556: t = &oMSZ[i];
557: OID(t) = O_N; NID(t) = N_Q; SGN(t) = -1; NM(t) = &oPSN[i]; DN(t) = 0;
558: }
559: szinit = 1;
560: }
561:
562: optobj(p)
563: Obj *p;
564: {
565: Obj t;
566: int n;
567: DCP dc;
568:
569: if ( t = *p )
570: switch ( OID(t) ) {
571: case O_N:
572: if ( (NID(t)==N_Q) && INT(t) && (PL(NM((Q)t))==1) ) {
573: n = QTOS((Q)t);
574: if ( !szinit )
575: sz_init();
576: if ( n < 1000 )
577: *p = (Obj)(SGN((Q)t)>0?&oPSZ[n]:&oMSZ[n]);
578: }
579: break;
580: case O_P:
581: for ( dc = DC((P)t); dc; dc = NEXT(dc) ) {
582: optobj(&DEG(dc)); optobj(&COEF(dc));
583: }
584: break;
585: case O_R:
586: optobj(&NM((R)t)); optobj(&DN((R)t)); break;
587: default:
588: break;
589: }
590: }
591: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>