Annotation of OpenXM_contrib2/asir2000/builtin/miscf.c, Revision 1.3
1.3 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/builtin/miscf.c,v 1.2 2000/01/18 05:55:05 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},
1.3 ! noro 39: #if INET && !defined(VISUAL) && DO_PLOT
1.1 noro 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));
1.3 ! noro 248: #if defined(VISUAL)
1.1 noro 249: set_error(code,reason,action);
1.3 ! noro 250: #endif
1.1 noro 251: error("");
252: *rp = 0;
253: }
254:
255: void Pflist(rp)
256: LIST *rp;
257: {
258: char *n;
259: STRING name;
260: NODE t,r,r0;
261: LIST l;
262:
263: for ( t = usrf, r0 = 0; t; t = NEXT(t) )
264: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
265: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
266: MKNODE(r,name,r0); r0 = r;
267: }
268: for ( t = ubinf; t; t = NEXT(t) )
269: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
270: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
271: MKNODE(r,name,r0); r0 = r;
272: }
273: for ( t = sysf; t; t = NEXT(t) )
274: if ( ((FUNC)BDY(t))->id != A_UNDEF ) {
275: n = NAME((FUNC)BDY(t)); MKSTR(name,n);
276: MKNODE(r,name,r0); r0 = r;
277: }
278: MKLIST(l,r0); *rp = l;
279: }
280:
281: void Pdelete_history(arg,rp)
282: NODE arg;
283: Q *rp;
284: {
285: switch ( argc(arg) ) {
286: case 0: default:
287: delete_history(0,(int)APVS->n);
288: break;
289: case 1:
290: delete_history(QTOS((Q)ARG0(arg)),1);
291: break;
292: }
293: *rp = 0;
294: }
295:
296: void delete_history(start,n)
297: int start,n;
298: {
299: int i,max;
300:
301: max = APVS->n;
302: if ( start < 0 || start >= max )
303: return;
304: if ( start + n > max )
305: n = max - start;
306: for ( i = 0; i < n; i++ )
307: APVS->va[start+i].priv = 0;
308: }
309:
310: void Ppause(rp)
311: LIST *rp;
312: {
313: char buf[BUFSIZ];
314:
315: fgets(buf,BUFSIZ,stdin);
316: *rp = 0;
317: }
318:
319: void Pgc(rp)
320: LIST *rp;
321: {
322: GC_gcollect();
323: *rp = 0;
324: }
325:
326: int exec_file(char *,char *);
327:
328: void Pbatch(arg,rp)
329: NODE arg;
330: Q *rp;
331: {
332: int ret;
333:
334: ret = exec_file(BDY((STRING)ARG0(arg)),BDY((STRING)ARG1(arg)));
335: STOQ(ret,*rp);
336: }
337:
1.3 ! noro 338: #if INET && !defined(VISUAL) && DO_PLOT
1.1 noro 339: void Pxpause(rp)
340: Q *rp;
341: {
342: if ( !init_display() )
343: *rp = 0;
344: else {
345: grab_pointer(); *rp = ONE;
346: }
347: }
348:
349: static Display *display;
350: static Window rootwin;
351:
352: init_display()
353: {
354: char *dname;
355: unsigned int tmp;
356: static int initialized;
357: int argc;
358: char *argv[1];
359:
360: if ( initialized )
361: return 1;
362: else
363: initialized = 1;
364: dname = (char *)getenv("DISPLAY");
365:
366: display = XOpenDisplay(dname);
367: if ( !display ) {
368: fprintf(stderr,"Can't open display\n");
369: return 0;
370: }
371: rootwin = RootWindow(display,DefaultScreen(display));
372: }
373:
374: grab_pointer()
375: {
376: XEvent ev;
377: static Cursor cursor;
378:
379: if ( !cursor )
380: cursor = XCreateFontCursor(display,XC_leftbutton);
381: XGrabPointer(display,rootwin,True,ButtonPressMask,GrabModeAsync,GrabModeAsync,None,cursor,CurrentTime);
382: while ( 1 ) {
383: XNextEvent(display,&ev);
384: if ( ev.xany.type == ButtonPress )
385: break;
386: }
387: XUngrabPointer(display,CurrentTime);
388: XSync(display,False);
389: return;
390: }
391: #endif
392:
393: void Psend_progress(NODE arg,Q *rp)
394: {
395: #if defined(VISUAL)
396: short per;
397: char *msg;
398:
399: per = (short)QTOS((Q)BDY(arg)); arg = NEXT(arg);
400: if ( arg )
401: msg = BDY((STRING)BDY(arg));
402: else
403: msg = "";
404: send_progress(per,msg);
405: #endif
406: *rp = 0;
407: }
408:
409: #if 0
410: static int optimize;
411: static struct oN oPSN[1000];
412: static struct oQ oPSZ[1000],oMSZ[1000];
413: static szinit = 0;
414:
415: void Popt(arg,rp)
416: NODE arg;
417: pointer *rp;
418: {
419: optimize = ARG0(arg) ? 1 : 0; *rp = 0;
420: }
421:
422:
423: void sz_init() {
424: int i;
425: Q t;
426:
427: for ( i = 1; i < 1000; i++ ) {
428: oPSN[i].p = 1; oPSN[i].b[0] = i;
429: t = &oPSZ[i];
430: OID(t) = O_N; NID(t) = N_Q; SGN(t) = 1; NM(t) = &oPSN[i]; DN(t) = 0;
431: t = &oMSZ[i];
432: OID(t) = O_N; NID(t) = N_Q; SGN(t) = -1; NM(t) = &oPSN[i]; DN(t) = 0;
433: }
434: szinit = 1;
435: }
436:
437: optobj(p)
438: Obj *p;
439: {
440: Obj t;
441: int n;
442: DCP dc;
443:
444: if ( t = *p )
445: switch ( OID(t) ) {
446: case O_N:
447: if ( (NID(t)==N_Q) && INT(t) && (PL(NM((Q)t))==1) ) {
448: n = QTOS((Q)t);
449: if ( !szinit )
450: sz_init();
451: if ( n < 1000 )
452: *p = (Obj)(SGN((Q)t)>0?&oPSZ[n]:&oMSZ[n]);
453: }
454: break;
455: case O_P:
456: for ( dc = DC((P)t); dc; dc = NEXT(dc) ) {
457: optobj(&DEG(dc)); optobj(&COEF(dc));
458: }
459: break;
460: case O_R:
461: optobj(&NM((R)t)); optobj(&DN((R)t)); break;
462: default:
463: break;
464: }
465: }
466: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>