Annotation of OpenXM_contrib2/asir2000/builtin/miscf.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/builtin/miscf.c,v 1.1.1.1 1999/12/03 07:39:07 noro Exp $ */
1.1 noro 2: #include "ca.h"
3: #include "parse.h"
4: #if INET && !defined(VISUAL)
5: #include <X11/Xlib.h>
6: #include <X11/cursorfont.h>
7: #endif
8:
9: void Pquit(), Pdebug(), Pnmono(), Pnez(), Popt(), Pshell(), Pheap();
10: void Perror(), Perror3(), Pversion(), Pflist(), Pdelete_history(), Ppause(), Pxpause();
11: void Pr2g(), Pread_cmo(), Pwrite_cmo();
12: void Pgc(),Pbatch(),Psend_progress();
13: void Pnull_command();
14: void Pgetenv();
1.2 ! noro 15: void Plib_ox_push_cmo(),Plib_ox_pop_cmo(),Plib_ox_push_cmd();
! 16: void Plib_ox_execute_string();
1.1 noro 17:
18: void delete_history(int,int);
19:
20: struct ftab misc_tab[] = {
21: {"null_command",Pnull_command,-99999},
22: {"getenv",Pgetenv,1},
23: {"end",Pquit,0},
24: {"quit",Pquit,0},
25: {"debug",Pdebug,0},
26: {"shell",Pshell,-2},
27: {"heap",Pheap,-1},
28: {"version",Pversion,0},
29: {"nmono",Pnmono,1},
30: {"error",Perror,1},
31: {"error3",Perror3,3},
32: {"nez",Pnez,1},
33: {"flist",Pflist,0},
34: {"delete_history",Pdelete_history,-2},
35: {"pause",Ppause,0},
36: {"gc",Pgc,0},
37: {"batch",Pbatch,2},
38: {"send_progress",Psend_progress,-2},
39: #if INET && !defined(VISUAL)
40: {"xpause",Pxpause,0},
41: #endif
42: #if 0
43: {"opt",Popt,1},
44: #endif
1.2 ! noro 45: /* test functions for library mode ox operations */
! 46: {"lib_ox_push_cmo",Plib_ox_push_cmo,1},
! 47: {"lib_ox_pop_cmo",Plib_ox_pop_cmo,0},
! 48: {"lib_ox_push_cmd",Plib_ox_push_cmd,1},
! 49: {"lib_ox_execute_string",Plib_ox_execute_string,1},
1.1 noro 50: {0,0,0},
51: };
1.2 ! noro 52:
! 53: extern int little_endian;
! 54:
! 55: int lib_ox_initialized;
! 56:
! 57: void Plib_ox_push_cmo(arg,rp)
! 58: NODE arg;
! 59: Q *rp;
! 60: {
! 61: void *cmo;
! 62:
! 63: if ( !lib_ox_initialized ) asir_ox_io_init(little_endian);
! 64: risa_to_cmo(ARG0(arg),&cmo);
! 65: asir_ox_push_cmo(cmo);
! 66: *rp = ONE;
! 67: }
! 68:
! 69: void Plib_ox_pop_cmo(rp)
! 70: Obj *rp;
! 71: {
! 72: void *buf;
! 73: int ret,len;
! 74:
! 75: if ( !lib_ox_initialized ) asir_ox_io_init(little_endian);
! 76: len = asir_ox_peek_cmo_size();
! 77: buf = (void *)MALLOC_ATOMIC(len);
! 78: ret = asir_ox_pop_cmo(buf,len);
! 79: if ( ret < 0 )
! 80: error("lib_ox_pop_cmo : buffer too small (cannot happen!)");
! 81: cmo_to_risa(buf,rp);
! 82: }
! 83:
! 84: void Plib_ox_push_cmd(arg,rp)
! 85: NODE arg;
! 86: Q *rp;
! 87: {
! 88: if ( !lib_ox_initialized ) asir_ox_io_init(little_endian);
! 89: asir_ox_push_cmd((unsigned int)QTOS((Q)ARG0(arg)));
! 90: *rp = ONE;
! 91: }
! 92:
! 93: void Plib_ox_execute_string(arg,rp)
! 94: NODE arg;
! 95: Q *rp;
! 96: {
! 97: if ( !lib_ox_initialized ) asir_ox_io_init(little_endian);
! 98: asir_ox_execute_string(BDY((STRING)ARG0(arg)));
! 99: *rp = ONE;
! 100: }
1.1 noro 101:
102: void Pgetenv(arg,rp)
103: NODE arg;
104: STRING *rp;
105: {
106: char *e,*f;
107: int len;
108:
109: e = (char *)getenv(BDY((STRING)ARG0(arg)));
110: if ( e ) {
111: len = strlen(e);
112: f = (char *)MALLOC_ATOMIC(len+1);
113: strcpy(f,e);
114: MKSTR(*rp,f);
115: } else
116: *rp = 0;
117: }
118:
119: void Pnull_command(arg,rp)
120: NODE arg;
121: Q *rp;
122: {
123: *rp = 0;
124: }
125:
126: void Pquit(rp)
127: pointer *rp;
128: {
129: if ( !NEXT(asir_infile) )
130: asir_terminate(2);
131: else {
132: closecurrentinput();
133: if ( !asir_infile->fp )
134: asir_terminate(2);
135: }
136: *rp = 0;
137: }
138:
139: void Pdebug(rp)
140: pointer *rp;
141: {
142: debug(0); *rp = 0;
143: }
144:
145: void Pshell(arg,rp)
146: NODE arg;
147: Q *rp;
148: {
149: char *com = 0;
150: char *pstr = 0;
151: int status;
152:
153: if ( arg ) {
154: asir_assert(ARG0(arg),O_STR,"shell");
155: com = BDY((STRING)ARG0(arg));
156: if ( NEXT(arg) )
157: pstr = BDY((STRING)ARG1(arg));
158: }
159: status = system(com);
160: STOQ(status,*rp);
161: }
162:
163: void Pnmono(arg,rp)
164: NODE arg;
165: Q *rp;
166: {
167: Obj obj;
168: int n;
169:
170: obj = (Obj)ARG0(arg);
171: if ( !obj || OID(obj) > O_R )
172: *rp = 0;
173: else
174: switch (OID(obj)) {
175: case O_N: case O_P:
176: n = nmonop((P)obj); STOQ(n,*rp); break;
177: case O_R:
178: n = nmonop(NM((R)obj)) + nmonop(DN((R)obj));
179: STOQ(n,*rp); break;
180: }
181: }
182:
183: void Pheap(arg,rp)
184: NODE arg;
185: Q *rp;
186: {
187: int h0,h;
188: void GC_expand_hp(int);
189:
190: h0 = get_heapsize();
191: if ( arg ) {
192: h = QTOS((Q)ARG0(arg));
193: if ( h > h0 )
194: GC_expand_hp(h-h0);
195: }
196: h = get_heapsize();
197: STOQ(h,*rp);
198: }
199:
200: unsigned int get_asir_version();
201:
202: void Pversion(rp)
203: Q *rp;
204: {
205: unsigned int version;
206:
207: version = get_asir_version();
208: STOQ(version,*rp);
209: }
210:
211: extern int nez;
212:
213: void Pnez(arg,rp)
214: NODE arg;
215: pointer *rp;
216: {
217: nez = ARG0(arg) ? 1 : 0; *rp = 0;
218: }
219:
220: void Perror(arg,rp)
221: NODE arg;
222: Q *rp;
223: {
224: char *s;
225:
226: if ( !arg || !ARG0(arg) || (OID((Obj)ARG0(arg)) != O_STR) )
227: s = "";
228: else
229: s = BDY((STRING)ARG0(arg));
230: error(s);
231: *rp = 0;
232: }
233:
234: void Perror3(arg,rp)
235: NODE arg;
236: Q *rp;
237: {
238: char s[BUFSIZ];
239: int code;
240: char *reason,*action;
241:
242: asir_assert(ARG0(arg),O_N,"error3");
243: asir_assert(ARG1(arg),O_STR,"error3");
244: asir_assert(ARG2(arg),O_STR,"error3");
245: code = QTOS((Q)ARG0(arg));
246: reason = BDY((STRING)ARG1(arg));
247: action = BDY((STRING)ARG2(arg));
248: set_error(code,reason,action);
249: error("");
250: *rp = 0;
251: }
252:
253: void Pflist(rp)
254: LIST *rp;
255: {
256: char *n;
257: STRING name;
258: NODE t,r,r0;
259: LIST l;
260:
261: for ( t = usrf, r0 = 0; t; t = NEXT(t) )
262: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
263: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
264: MKNODE(r,name,r0); r0 = r;
265: }
266: for ( t = ubinf; t; t = NEXT(t) )
267: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
268: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
269: MKNODE(r,name,r0); r0 = r;
270: }
271: for ( t = sysf; t; t = NEXT(t) )
272: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
273: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
274: MKNODE(r,name,r0); r0 = r;
275: }
276: MKLIST(l,r0); *rp = l;
277: }
278:
279: void Pdelete_history(arg,rp)
280: NODE arg;
281: Q *rp;
282: {
283: switch ( argc(arg) ) {
284: case 0: default:
285: delete_history(0,(int)APVS->n);
286: break;
287: case 1:
288: delete_history(QTOS((Q)ARG0(arg)),1);
289: break;
290: }
291: *rp = 0;
292: }
293:
294: void delete_history(start,n)
295: int start,n;
296: {
297: int i,max;
298:
299: max = APVS->n;
300: if ( start < 0 || start >= max )
301: return;
302: if ( start + n > max )
303: n = max - start;
304: for ( i = 0; i < n; i++ )
305: APVS->va[start+i].priv = 0;
306: }
307:
308: void Ppause(rp)
309: LIST *rp;
310: {
311: char buf[BUFSIZ];
312:
313: fgets(buf,BUFSIZ,stdin);
314: *rp = 0;
315: }
316:
317: void Pgc(rp)
318: LIST *rp;
319: {
320: GC_gcollect();
321: *rp = 0;
322: }
323:
324: int exec_file(char *,char *);
325:
326: void Pbatch(arg,rp)
327: NODE arg;
328: Q *rp;
329: {
330: int ret;
331:
332: ret = exec_file(BDY((STRING)ARG0(arg)),BDY((STRING)ARG1(arg)));
333: STOQ(ret,*rp);
334: }
335:
336: #if INET && !defined(VISUAL)
337: void Pxpause(rp)
338: Q *rp;
339: {
340: if ( !init_display() )
341: *rp = 0;
342: else {
343: grab_pointer(); *rp = ONE;
344: }
345: }
346:
347: static Display *display;
348: static Window rootwin;
349:
350: init_display()
351: {
352: char *dname;
353: unsigned int tmp;
354: static int initialized;
355: int argc;
356: char *argv[1];
357:
358: if ( initialized )
359: return 1;
360: else
361: initialized = 1;
362: dname = (char *)getenv("DISPLAY");
363:
364: display = XOpenDisplay(dname);
365: if ( !display ) {
366: fprintf(stderr,"Can't open display\n");
367: return 0;
368: }
369: rootwin = RootWindow(display,DefaultScreen(display));
370: }
371:
372: grab_pointer()
373: {
374: XEvent ev;
375: static Cursor cursor;
376:
377: if ( !cursor )
378: cursor = XCreateFontCursor(display,XC_leftbutton);
379: XGrabPointer(display,rootwin,True,ButtonPressMask,GrabModeAsync,GrabModeAsync,None,cursor,CurrentTime);
380: while ( 1 ) {
381: XNextEvent(display,&ev);
382: if ( ev.xany.type == ButtonPress )
383: break;
384: }
385: XUngrabPointer(display,CurrentTime);
386: XSync(display,False);
387: return;
388: }
389: #endif
390:
391: void Psend_progress(NODE arg,Q *rp)
392: {
393: #if defined(VISUAL)
394: short per;
395: char *msg;
396:
397: per = (short)QTOS((Q)BDY(arg)); arg = NEXT(arg);
398: if ( arg )
399: msg = BDY((STRING)BDY(arg));
400: else
401: msg = "";
402: send_progress(per,msg);
403: #endif
404: *rp = 0;
405: }
406:
407: #if 0
408: static int optimize;
409: static struct oN oPSN[1000];
410: static struct oQ oPSZ[1000],oMSZ[1000];
411: static szinit = 0;
412:
413: void Popt(arg,rp)
414: NODE arg;
415: pointer *rp;
416: {
417: optimize = ARG0(arg) ? 1 : 0; *rp = 0;
418: }
419:
420:
421: void sz_init() {
422: int i;
423: Q t;
424:
425: for ( i = 1; i < 1000; i++ ) {
426: oPSN[i].p = 1; oPSN[i].b[0] = i;
427: t = &oPSZ[i];
428: OID(t) = O_N; NID(t) = N_Q; SGN(t) = 1; NM(t) = &oPSN[i]; DN(t) = 0;
429: t = &oMSZ[i];
430: OID(t) = O_N; NID(t) = N_Q; SGN(t) = -1; NM(t) = &oPSN[i]; DN(t) = 0;
431: }
432: szinit = 1;
433: }
434:
435: optobj(p)
436: Obj *p;
437: {
438: Obj t;
439: int n;
440: DCP dc;
441:
442: if ( t = *p )
443: switch ( OID(t) ) {
444: case O_N:
445: if ( (NID(t)==N_Q) && INT(t) && (PL(NM((Q)t))==1) ) {
446: n = QTOS((Q)t);
447: if ( !szinit )
448: sz_init();
449: if ( n < 1000 )
450: *p = (Obj)(SGN((Q)t)>0?&oPSZ[n]:&oMSZ[n]);
451: }
452: break;
453: case O_P:
454: for ( dc = DC((P)t); dc; dc = NEXT(dc) ) {
455: optobj(&DEG(dc)); optobj(&COEF(dc));
456: }
457: break;
458: case O_R:
459: optobj(&NM((R)t)); optobj(&DN((R)t)); break;
460: default:
461: break;
462: }
463: }
464: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>