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