1 | #include "lisp.hpp" |
---|
2 | #include "lisp_gc.hpp" |
---|
3 | #include <ctype.h> |
---|
4 | |
---|
5 | #include <unistd.h> |
---|
6 | #include <stdio.h> |
---|
7 | #include <string.h> |
---|
8 | #include <stdlib.h> |
---|
9 | |
---|
10 | #if defined( __POWERPC__ ) |
---|
11 | |
---|
12 | #elif defined( __WATCOMC__ ) |
---|
13 | #include <sys\types.h> |
---|
14 | #include <direct.h> |
---|
15 | #define make_dir(dir) mkdir(dir) |
---|
16 | #else |
---|
17 | #include <sys/stat.h> |
---|
18 | #define make_dir(dir) mkdir(dir,S_IRWXU | S_IRWXG | S_IRWXO) |
---|
19 | |
---|
20 | void modify_install_path(char *path) { ; } |
---|
21 | #endif |
---|
22 | |
---|
23 | |
---|
24 | #ifdef __WATCOMC__ |
---|
25 | void modify_install_path(char *path) |
---|
26 | { |
---|
27 | char ret[100],*r,*i_path=path; |
---|
28 | int dc=0; |
---|
29 | r=ret; |
---|
30 | |
---|
31 | if (path[1]==':') // if "c:\blahblah " skip the c: |
---|
32 | { |
---|
33 | r[0]=path[0]; |
---|
34 | r[1]=path[1]; |
---|
35 | r+=2; |
---|
36 | path+=2; |
---|
37 | } |
---|
38 | |
---|
39 | while (*path) // eliminate double slashes and reduce more than 8 char dirs |
---|
40 | { |
---|
41 | |
---|
42 | if (*path=='\\' || *path=='/') |
---|
43 | { |
---|
44 | dc=0; |
---|
45 | *r=*path; |
---|
46 | r++; |
---|
47 | path++; |
---|
48 | while (*path=='\\' || *path=='/') path++; |
---|
49 | } else if (dc<8) |
---|
50 | { |
---|
51 | dc++; |
---|
52 | *r=*path; |
---|
53 | r++; |
---|
54 | path++; |
---|
55 | } else path++; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | *r=0; |
---|
60 | strcpy(i_path,ret); |
---|
61 | } |
---|
62 | |
---|
63 | #include <dos.h> |
---|
64 | #endif |
---|
65 | |
---|
66 | int change_dir(char *path) |
---|
67 | { |
---|
68 | #ifdef __WATCOMC__ |
---|
69 | unsigned cur_drive; |
---|
70 | _dos_getdrive(&cur_drive); |
---|
71 | if (path[1]==':') |
---|
72 | { |
---|
73 | unsigned total; |
---|
74 | _dos_setdrive(toupper(path[0])-'A'+1,&total); |
---|
75 | |
---|
76 | |
---|
77 | unsigned new_drive; |
---|
78 | _dos_getdrive(&new_drive); |
---|
79 | |
---|
80 | if (new_drive!=toupper(path[0])-'A'+1) |
---|
81 | { |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | path+=2; |
---|
86 | } |
---|
87 | |
---|
88 | int er=chdir(path); |
---|
89 | if (er) |
---|
90 | { |
---|
91 | unsigned total; |
---|
92 | _dos_setdrive(cur_drive,&total); |
---|
93 | } |
---|
94 | return !er; |
---|
95 | #else |
---|
96 | int ret=chdir(path); // weird |
---|
97 | ret=chdir(path); |
---|
98 | return ret==0; |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | long K_avail(char *path); |
---|
103 | |
---|
104 | #ifdef __WATCOMC__ |
---|
105 | #include "i86.h" |
---|
106 | #include <conio.h> |
---|
107 | #include <bios.h> |
---|
108 | |
---|
109 | long K_avail(char *path) |
---|
110 | { |
---|
111 | unsigned drive=0; |
---|
112 | if (path[1]==':') drive=toupper(path[0])-'A'+1; |
---|
113 | else _dos_getdrive(&drive); |
---|
114 | |
---|
115 | struct diskfree_t f; |
---|
116 | _dos_getdiskfree(drive,&f); |
---|
117 | |
---|
118 | return (long)((long)f.sectors_per_cluster*(long)f.bytes_per_sector/1024)*(long)f.avail_clusters; |
---|
119 | } |
---|
120 | |
---|
121 | void set_cursor(int x, int y) { |
---|
122 | union REGS in,out; |
---|
123 | memset(&in,0,sizeof(in)); |
---|
124 | in.w.ax=0x0200; in.w.bx=0; in.w.dx=(y<<8)|x; |
---|
125 | int386(0x10,&in,&in); |
---|
126 | } |
---|
127 | |
---|
128 | void put_char(int x, int y, int val, int color) { *((unsigned short *)(0xb8000+y*2*80+x*2))=(val)|(color<<8); } |
---|
129 | unsigned short get_char(int x, int y, int val) { return *((unsigned short *)(0xb8000+y*2*80+x*2)); } |
---|
130 | void put_string(int x,int y,char *s, int c) { while (*s) put_char(x++,y,*(s++),c); } |
---|
131 | void bar(int x1, int y1, int x2, int y2, int v, int c) |
---|
132 | { int x,y; |
---|
133 | for (x=x1;x<=x2;x++) |
---|
134 | for (y=y1;y<=y2;y++) |
---|
135 | put_char(x,y,v,c); |
---|
136 | } |
---|
137 | |
---|
138 | void cls() { bar(0,0,79,25,' ',0x07); set_cursor(0,0); } |
---|
139 | |
---|
140 | void box(int x1, int y1, int x2, int y2, int c) |
---|
141 | { |
---|
142 | unsigned char bc[]={ 218,191,217,192,196,179 }; |
---|
143 | put_char(x1,y1,bc[0],c); |
---|
144 | put_char(x2,y1,bc[1],c); |
---|
145 | put_char(x2,y2,bc[2],c); |
---|
146 | put_char(x1,y2,bc[3],c); |
---|
147 | int x; for (x=x1+1;x<x2;x++) { put_char(x,y1,bc[4],c); put_char(x,y2,bc[4],c); } |
---|
148 | int y; for (y=y1+1;y<y2;y++) { put_char(x1,y,bc[5],c); put_char(x2,y,bc[5],c); } |
---|
149 | } |
---|
150 | |
---|
151 | void put_title(char *t) |
---|
152 | { |
---|
153 | int x1=0,y1=0,x2=79,y2=25; |
---|
154 | bar(x1,y1,x2,y1,' ',0x4f); |
---|
155 | put_string((x1+x2)/2-strlen(t)/2,y1,t,0x4f); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | int nice_copy(char *title, char *source, char *dest) |
---|
162 | { |
---|
163 | int x1=0,y1=0,x2=79,y2=25; |
---|
164 | bar(x1,y1+1,x2,y2,176,0x01); |
---|
165 | put_title(title); |
---|
166 | |
---|
167 | box(x1,(y1+y2)/2-1,x2,(y1+y2)/2+3,0x17); |
---|
168 | bar(x1+1,(y1+y2)/2,x2-1,(y1+y2)/2+2,' ',0x17); |
---|
169 | |
---|
170 | char msg[100]; |
---|
171 | sprintf(msg,"Copying %s -> %s",source,dest); |
---|
172 | put_string(x1+1,(y1+y2)/2,msg,0x17); |
---|
173 | bar(x1+1,(y1+y2)/2+2,x2-1,(y1+y2)/2+2,176,0x17); |
---|
174 | |
---|
175 | char *buffer=(char *)jmalloc(0xf000,"read buf"); |
---|
176 | if (!buffer) return 0; |
---|
177 | FILE *out=fopen(dest,"wb"); |
---|
178 | if (!out) { jfree(buffer) ; return 0; } |
---|
179 | FILE *in=fopen(source,"rb"); |
---|
180 | if (!in) { jfree(buffer); fclose(out); unlink(dest); return 0; } |
---|
181 | |
---|
182 | fseek(in,0,SEEK_END); |
---|
183 | long size=ftell(in); |
---|
184 | fseek(in,0,SEEK_SET); |
---|
185 | int osize=size; |
---|
186 | while (size) |
---|
187 | { |
---|
188 | long tr=fread(buffer,1,0xf000,in); |
---|
189 | bar(x1+1,(y1+y2)/2+2,x1+1+(x2-x1)*(osize-size-tr)/osize,(y1+y2)/2+2,178,0x17); |
---|
190 | |
---|
191 | if (fwrite(buffer,1,tr,out)!=tr) |
---|
192 | { |
---|
193 | fclose(out); |
---|
194 | fclose(in); |
---|
195 | unlink(dest); |
---|
196 | jfree(buffer); |
---|
197 | return 0; |
---|
198 | } |
---|
199 | size-=tr; |
---|
200 | |
---|
201 | } |
---|
202 | fclose(in); |
---|
203 | fclose(out); |
---|
204 | jfree(buffer); |
---|
205 | cls(); |
---|
206 | return 1; |
---|
207 | } |
---|
208 | |
---|
209 | void *nice_input(char *t, char *p, char *d) |
---|
210 | { |
---|
211 | |
---|
212 | int x1=0,y1=0,x2=79,y2=25; |
---|
213 | bar(x1,y1+1,x2,y2,176,0x01); |
---|
214 | put_title(t); |
---|
215 | |
---|
216 | box(x1,(y1+y2)/2-1,x2,(y1+y2)/2+1,0x17); |
---|
217 | bar(x1+1,(y1+y2)/2,x2-1,(y1+y2)/2,' ',0x17); |
---|
218 | |
---|
219 | put_string(x1+1,(y1+y2)/2,p,0x17); |
---|
220 | bar (x1+1+strlen(p)+1,(y1+y2)/2,x2-1,(y1+y2)/2,' ',0x0f); |
---|
221 | put_string(x1+1+strlen(p)+1,(y1+y2)/2,d,0x70); |
---|
222 | set_cursor(x1+1+strlen(p)+1,(y1+y2)/2); |
---|
223 | while (!_bios_keybrd(_KEYBRD_READY)); |
---|
224 | if (_bios_keybrd(_KEYBRD_READY)==7181) |
---|
225 | { |
---|
226 | getch(); |
---|
227 | cls(); |
---|
228 | char ln[100]; |
---|
229 | strcpy(ln,d); // d might get collect in next new |
---|
230 | return new_lisp_string(ln); |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | bar (x1+1+strlen(p)+1,(y1+y2)/2,x2-1,(y1+y2)/2,' ',0x0f); |
---|
235 | int key; |
---|
236 | char ln[100]; |
---|
237 | ln[0]=0; |
---|
238 | do |
---|
239 | { |
---|
240 | key=getch(); |
---|
241 | if (key==8 && ln[0]) |
---|
242 | { |
---|
243 | ln[strlen(ln)-1]=0; |
---|
244 | bar(x1+1+strlen(p)+1,(y1+y2)/2,x2-1,(y1+y2)/2,' ',0x0f); |
---|
245 | put_string(x1+1+strlen(p)+1,(y1+y2)/2,ln,0x0f); |
---|
246 | } |
---|
247 | else if (isprint(key)) |
---|
248 | { |
---|
249 | ln[strlen(ln)+1]=0; |
---|
250 | ln[strlen(ln)]=key; |
---|
251 | |
---|
252 | put_string(x1+1+strlen(p)+1,(y1+y2)/2,ln,0x0f); |
---|
253 | } |
---|
254 | set_cursor(x1+1+strlen(p)+1+strlen(ln),(y1+y2)/2); |
---|
255 | } while (key!=13 && key!=27); |
---|
256 | |
---|
257 | cls(); |
---|
258 | if (key==27) return NULL; |
---|
259 | return new_lisp_string(ln); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | void *nice_menu(void *main_title, void *menu_title, void *list) |
---|
264 | { |
---|
265 | int x1=0,y1=0,x2=79,y2=25; |
---|
266 | |
---|
267 | p_ref r1(main_title),r2(menu_title),r3(list); |
---|
268 | main_title=eval(main_title); |
---|
269 | menu_title=eval(menu_title); |
---|
270 | char *t=lstring_value(main_title); |
---|
271 | put_title(t); |
---|
272 | |
---|
273 | bar(x1,y1+1,x2,y2,176,0x01); |
---|
274 | |
---|
275 | void *l=eval(list); |
---|
276 | p_ref r4(l); |
---|
277 | int menu_height=list_length(l),menu_length=0; |
---|
278 | void *m; |
---|
279 | for (m=l;m;m=CDR(m)) |
---|
280 | { long l=strlen(lstring_value(CAR(m))); |
---|
281 | if (l>menu_length) menu_length=l; |
---|
282 | } |
---|
283 | |
---|
284 | char *mt=lstring_value(menu_title); |
---|
285 | if (strlen(mt)>menu_length) menu_length=strlen(mt); |
---|
286 | |
---|
287 | int mx=(x2+x1)/2-menu_length/2-1,my=(y2+y1)/2-menu_height/2-2; |
---|
288 | box(mx,my,mx+menu_length+2,my+menu_height+3,0x17); |
---|
289 | bar(mx+1,my+1,mx+menu_length+1,my+menu_height+2,' ',0x17); |
---|
290 | put_string(mx+1+menu_length/2-strlen(mt)/2,my,mt,0x1f); // draw menu title |
---|
291 | set_cursor(0,25); |
---|
292 | |
---|
293 | int cur_sel=0; |
---|
294 | int key; |
---|
295 | do |
---|
296 | { |
---|
297 | int y=0; |
---|
298 | for (m=l;m;m=CDR(m),y++) |
---|
299 | { |
---|
300 | char *s=lstring_value(CAR(m)); |
---|
301 | if (y==cur_sel) |
---|
302 | { |
---|
303 | bar(mx+1,my+2+y,mx+1+menu_length,my+2+y,' ',0x0f); |
---|
304 | put_string(mx+1+menu_length/2-strlen(s)/2,my+2+y,s,0x0f); |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | bar(mx+1,my+2+y,mx+1+menu_length,my+2+y,' ',0x1f); |
---|
309 | put_string(mx+1+menu_length/2-strlen(s)/2,my+2+y,s,0x1f); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | key=_bios_keybrd(_KEYBRD_READ); |
---|
314 | |
---|
315 | if (key==18432) |
---|
316 | { cur_sel--; if (cur_sel==-1) cur_sel=menu_height-1; } |
---|
317 | if (key==20480) |
---|
318 | { cur_sel++; if (cur_sel==menu_height) cur_sel=0; } |
---|
319 | |
---|
320 | } while (key!=283 && key!=7181); |
---|
321 | cls(); |
---|
322 | if (key==283) |
---|
323 | return new_lisp_number(-1); |
---|
324 | |
---|
325 | return new_lisp_number(cur_sel); |
---|
326 | } |
---|
327 | |
---|
328 | void center_tbox(void *list, int c) |
---|
329 | { |
---|
330 | int x1=0,y1=0,x2=79,y2=25; |
---|
331 | int h; |
---|
332 | |
---|
333 | if (item_type(list)==L_CONS_CELL) |
---|
334 | h=list_length(list); |
---|
335 | else h=1; |
---|
336 | |
---|
337 | int y=(y1+y2)/2-h/2+1; |
---|
338 | box(x1,(y1+y2)/2-h/2,x2,(y1+y2)/2-h/2+h+1,c); |
---|
339 | bar(x1+1,y,x2-1,(y1+y2)/2-h/2+h+1-1,' ',c); |
---|
340 | |
---|
341 | if (item_type(list)==L_CONS_CELL) |
---|
342 | { |
---|
343 | while (list) |
---|
344 | { |
---|
345 | char *s=lstring_value(CAR(list)); |
---|
346 | put_string((x1+x2)/2-strlen(s)/2,y++,s,c); |
---|
347 | list=CDR(list); |
---|
348 | } |
---|
349 | } else |
---|
350 | { |
---|
351 | char *s=lstring_value(list); |
---|
352 | put_string((x1+x2)/2-strlen(s)/2,y++,s,c); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | void *show_yes_no(void *t, void *msg, void *y, void *n) |
---|
357 | { |
---|
358 | p_ref r1(t),r2(msg),r3(y),r4(n); |
---|
359 | y=eval(y); |
---|
360 | n=eval(n); |
---|
361 | put_title(lstring_value(eval(t))); |
---|
362 | |
---|
363 | int x1=0,y1=0,x2=79,y2=25; |
---|
364 | bar(x1,y1+1,x2,y2,176,0x01); |
---|
365 | center_tbox(eval(msg),0x1f); |
---|
366 | int key; |
---|
367 | char *yes=lstring_value(y); |
---|
368 | char *no=lstring_value(n); |
---|
369 | set_cursor(0,25); |
---|
370 | do |
---|
371 | { |
---|
372 | key=getch(); |
---|
373 | set_cursor(0,0); |
---|
374 | } while (toupper(key)!=toupper(yes[0]) && toupper(key)!=toupper(no[0])); |
---|
375 | cls(); |
---|
376 | if (toupper(key)==toupper(yes[0])) |
---|
377 | return true_symbol; |
---|
378 | else return NULL; |
---|
379 | } |
---|
380 | |
---|
381 | #else |
---|
382 | |
---|
383 | int nice_copy(char *title, char *source, char *dest) { return 0; } |
---|
384 | |
---|
385 | long K_avail(char *path) |
---|
386 | { |
---|
387 | #if 0 // ugh |
---|
388 | char cmd[100]; |
---|
389 | sprintf(cmd,"du %s",path); |
---|
390 | FILE *fp=popen(cmd,"rb"); |
---|
391 | if (!fp) |
---|
392 | { |
---|
393 | pclose(fp); |
---|
394 | return 20000; |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | fgets(cmd,100,fp); |
---|
399 | if (feof(fp)) |
---|
400 | { |
---|
401 | pclose(fp); |
---|
402 | return 20000; |
---|
403 | } |
---|
404 | fgets(cmd,100,fp); |
---|
405 | char *s=cmd+strlen(cmd)-1; |
---|
406 | while (*s==' ' || *s=='\t') s--; |
---|
407 | while (*s!=' ' && *s!='\t') s--; // skip last field |
---|
408 | |
---|
409 | while (*s==' ' || *s=='\t') s--; |
---|
410 | while (*s!=' ' && *s!='\t') s--; // skip last field |
---|
411 | |
---|
412 | while (*s==' ' || *s=='\t') s--; |
---|
413 | while (*s!=' ' && *s!='\t') s--; s++; // skip last field |
---|
414 | |
---|
415 | long a; |
---|
416 | sscanf(s,"%d",&a); |
---|
417 | |
---|
418 | pclose(fp); |
---|
419 | return a; |
---|
420 | } |
---|
421 | #endif |
---|
422 | } |
---|
423 | |
---|
424 | void *show_yes_no(void *t, void *msg, void *y, void *n) |
---|
425 | { |
---|
426 | p_ref r1(t),r2(msg),r3(y),r4(n); |
---|
427 | t=eval(t); msg=eval(msg); y=eval(y); n=eval(n); |
---|
428 | int c; |
---|
429 | char *yes=lstring_value(y); |
---|
430 | char *no=lstring_value(n); |
---|
431 | do |
---|
432 | { |
---|
433 | printf("\n\n\n\n\n%s\n\n",lstring_value(t)); |
---|
434 | void *v=msg; |
---|
435 | if (item_type(v)==L_CONS_CELL) |
---|
436 | while (v) { printf("** %s\n",lstring_value(CAR(v))); v=CDR(v); } |
---|
437 | else printf("** %s\n",lstring_value(v)); |
---|
438 | char msg[100]; |
---|
439 | fgets(msg,100,stdin); |
---|
440 | c=msg[0]; |
---|
441 | } while (toupper(c)!=toupper(yes[0]) && toupper(c)!=toupper(no[0])); |
---|
442 | if (toupper(c)==toupper(yes[0])) |
---|
443 | return true_symbol; |
---|
444 | else return NULL; |
---|
445 | } |
---|
446 | |
---|
447 | void *nice_menu(void *main_title, void *menu_title, void *list) |
---|
448 | { |
---|
449 | p_ref r1(main_title),r2(menu_title),r3(list); |
---|
450 | main_title=eval(main_title); menu_title=eval(menu_title); list=eval(list); |
---|
451 | char msg[100]; |
---|
452 | int i=0,d; |
---|
453 | do |
---|
454 | { |
---|
455 | printf("\n\n\n\n\n%s\n\n%s\n-----------------------------------\n", |
---|
456 | lstring_value(main_title),lstring_value(menu_title)); |
---|
457 | |
---|
458 | void *v=list; |
---|
459 | for (;v;v=CDR(v),i++) |
---|
460 | { |
---|
461 | printf("%d) %s\n",i+1,lstring_value(CAR(v))); |
---|
462 | } |
---|
463 | fprintf(stderr,"> "); |
---|
464 | fgets(msg,100,stdin); |
---|
465 | sscanf(msg,"%d",&d); |
---|
466 | } while (d-1>=i && d<=0); |
---|
467 | return new_lisp_number(d-1); |
---|
468 | } |
---|
469 | |
---|
470 | void *nice_input(char *t, char *p, char *d) |
---|
471 | { |
---|
472 | int x; for (x=0;x<(40-strlen(t)/2);x++) printf(" "); |
---|
473 | printf("%s\n",t); |
---|
474 | for (x=0;x<78;x++) printf("-"); printf("\n"); |
---|
475 | fprintf(stderr,"%s (ENTER=%s) > ",p,d); |
---|
476 | char ln[100]; |
---|
477 | fgets(ln,100,stdin); ln[strlen(ln)-1]=0; |
---|
478 | if (ln[0]==0) |
---|
479 | { |
---|
480 | strcpy(ln,d); // d might get collect in next new |
---|
481 | return new_lisp_string(ln); |
---|
482 | } |
---|
483 | else |
---|
484 | return new_lisp_string(ln); |
---|
485 | } |
---|
486 | |
---|
487 | #endif |
---|