-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbigforth.c
1774 lines (1452 loc) · 36.4 KB
/
bigforth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* generic bigforth loader in C
is used for OS/2, Linux and Windows 95 */
#ifdef linux
#define _GNU_SOURCE
#endif
#if defined(BSD) || defined(linux) || defined(DARWIN)
#ifndef unix
#define unix
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#include <signal.h>
#ifdef unix
#include <locale.h>
#endif
#ifdef _WIN32
#include <windows.h>
HFILE sinp, sout, serr;
#endif
#define max(a, b) (a > b) ? a : b;
#define VERBOSE
int verbose=0;
#ifdef VERBOSE
# ifdef _WIN32
# define PRINTV(x...) if(verbose) { char buf[100]; sprintf(buf, x), \
_hwrite(sout, buf, strlen(buf)); }
# define PRINTVX(n, x...) if(verbose>=n) { char buf[100]; sprintf(buf, x), \
_hwrite(sout, buf, strlen(buf)); }
# else
# define PRINTV(x...) if(verbose) fprintf(stderr, x)
# define PRINTVX(n, x...) if(verbose>=n) fprintf(stderr, x)
# endif
#else
# define PRINTV(x...)
# define PRINTVX(n, x...)
#endif
/* OS-dependend part:
bigFORTH needs the following file system and memory management
primitives for loading:
get amount of availabe memory
allocate: read, write, stack and execute
open file by name (r/o, binary)
seek in a file to absolute position
read in a number of bytes
close the file
*/
#define CLEN(string) string,strlen(string)
#ifdef OS2 /* create OS/2 loader */
#define INCL_SUB
#define INCL_DOS
#include <os2.h>
#define OSFILE HFILE
#define available_mem(size) (size=0x01000000)
#define alloc_mem(size,heap) DosAllocMem((void *)&(heap), size, fALLOC)
/* (heap=(int *) sbrk(size)) */
#define open_by_name(name,handle) \
({ \
ULONG usAction=OPEN_ACTION_FAIL_IF_NEW; \
DosOpen(name, &handle, &usAction, 0L, 0, FILE_OPEN, 0x00C0, 0L); \
})
#define seek_to(handle,pos) \
({ \
long dummy; \
DosSetFilePtr(handle, pos, FILE_BEGIN, &dummy); \
dummy; \
})
#define read_bytes(handle,to,size) \
({ \
long dummy; \
DosRead(handle, to, size, &dummy); \
dummy; \
})
#define close_file(handle) DosClose(handle);
#define PANIC(string) \
({ \
long dummy; \
DosWrite(1, CLEN(string), &dummy); \
})
#define LOADER
#endif /* OS/2 */
#ifdef unix /* Create Unix loader */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
/* #include <asm/sigcontext.h> */
#include <string.h>
#define OSFILE int
int bytes=0;
static void* heapstart=(void*)0x10000000;
#define available_mem(size) (size=0x01000000)
#define alloc_mem(size,heap) (heap=(int *)mmap(heapstart, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0)); heapstart+=size;
#define open_by_name(name,handle) (handle=open(name, O_RDONLY))
#define seek_to(handle,pos) ({ long _pos = pos; if(bytes!=_pos) lseek(handle, _pos, SEEK_SET); bytes=_pos; })
#define read_bytes(handle,to,size) (bytes+=read(handle, to, size))
#define close_file(handle) (close(handle))
#define PANIC(string) \
(write(fileno(stderr), CLEN(string)))
#define LOADER
#endif
#ifdef _WIN32 /* Generic loader */
#define OSFILE HFILE
#define available_mem(size) (size=0x0100000)
#ifndef STACK_ALLOC
static void* heapstart=(void*)0x10000000;
#define alloc_mem(size,heap) (heap=(int *) VirtualAlloc(heapstart, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE)); heapstart+=size;
#else
int * stackalloc(int size)
{
asm("movl 8(%ebp),%eax\n\
stlop: cmpl $0,(%esp)\n\
addl $-0x1000,%esp\n\
addl $-0x1000,%eax\n\
jb stlop\n\
movl %esp,%eax\n\
leave\n\
ret");
}
#define alloc_mem(size,heap) (heap=stackalloc(size+0x1000))
#endif
#define open_by_name(name,handle) (handle=_lopen(name, OF_READ))
#define seek_to(handle,pos) (_llseek(handle, pos, FILE_BEGIN))
#define read_bytes(handle,to,size) (_lread(handle, to, size))
#define close_file(handle) (_lclose(handle))
#define PANIC(string) \
(_hwrite(serr, string, strlen(string)))
#define PANIC1(string, n) \
({ char buf[100], sprintf(buf, string, n); \
_hwrite(serr, buf, strlen(buf)) })
#undef linux
#define LOADER
#endif
/* end OS-dependend part */
typedef int BIGFORTH(int *, char *, void **);
typedef int THROW(int);
typedef BIGFORTH *PBIGFORTH;
typedef THROW *PTHROW;
char * cstring(char * string)
{
static char name[32];
memcpy(name, string+1, (int)(*string) & 0x1F);
name[(int)(*string)]=0;
return name;
}
static char bigforth_header[] =
#ifdef OS2
"\353\137"
#ifdef MINOS
"\062"
#else
"\040"
#endif
"ANS bigFORTH 386-OS/2 rev. "
#include "version.h"
#ifdef MINOS
"+MINOS rev. "
#include "minos-version.h"
#endif
"\022compiled 24jan97py"
#endif
#ifdef unix
"\353\137"
#ifdef MINOS
"\062"
#else
"\041"
#endif
"ANS bigFORTH 386-Linux rev. "
#include "version.h"
#ifdef MINOS
"+MINOS rev. "
#include "minos-version.h"
#endif
"\024compiled " VERSION_DATE "py"
#endif
#ifdef _WIN32
"\353\137"
#ifdef MINOS
"\062"
#else
"\041"
#endif
"ANS bigFORTH 386-Win32 rev. "
#include "version.h"
#ifdef MINOS
"+MINOS rev. "
#include "minos-version.h"
#endif
"\024compiled " VERSION_DATE "py"
#endif
"\011#00001py "
"\035(c) 1990-2003 by Bernd Paysan"
#ifdef MINOS
"\016MINOS+bigFORTH"
#else
"\016plain bigFORTH"
#endif
;
#define DEFAULTDICT 0x0080000
int bfdat[10]=
{
0, /* mroot */
0, /* heaps */
DEFAULTDICT, /* dictionary size */
0x00080000, /* Minimal */
0x04000000, /* Maximal */
0x00020000, /* Stacklen */
1, /* argc */
0, /* argv */
0, /* env */
0x00010000, /* Return stack len */
};
int recovery[32];
char fpdump[108];
jmp_buf throw_jmp_buf;
#define mroot ((int **)bfdat)[0]
#define heaps ((int **)bfdat)[1]
#define memdat (bfdat+2)
#define stlen (bfdat[5])
#define argc_ (bfdat[6])
#define argv_ ((char ***)bfdat)[7]
#define env_ ((char ***)bfdat)[8]
#define dict_ bfdat[2]
#define rstack_ bfdat[9]
#ifdef _WIN32
#define hinst_ *((HINSTANCE *)(((int *)bfdat)+9))
#define cmdsh_ ((int *)bfdat)[10]
#define win_ ((int *)bfdat)[11]
#define gc_ ((int *)bfdat)[12]
#define sp_ ((int *)bfdat)[13]
#endif
char fileerr[8]="where's ";
char file[1024];
char* linkinfo;
void makeempty(int * block, int n)
/* Marks the n bytes long block at block */
{
PRINTVX(5, "Blocking %08x size %08x\n", (int)block, n);
block[1]=0;
block[0]=n;
*(int *)(((char *)block)+n-sizeof(int))=n;
}
int * bf_alloc(int * free, int n)
/* returns next free: free+2 is pointer for object! */
{
int m;
PRINTVX(4, "Allocate %08x size %08x\n", (int)free, n);
n=(n+0x1B) & -0x10;
if(*free < n)
{
PANIC("Running out of Memory!\n");
exit(1);
}
m=*free-n;
makeempty(free,n);
free[1]=-1;
free=(int *)((int)free+n);
makeempty((int *)free,m);
return free;
}
int * find_module(char * name, int date, int * module)
{
int * retmodule;
PRINTVX(4, "Trying to find %s hash %08x", cstring(name), date);
PRINTVX(4, " in module %s (%08lx), cs %08x\n", cstring(((char*)module)+0x30),
(long) module, module[5]);
if(!memcmp(name,"\003nil",4)) return NULL;
do
{
if(module[5]==date && !memcmp(name,((char *)module)+0x30,1+*name))
return module;
PRINTVX(5, "Not found on this trial %s\n", cstring(((char*)module)+0x30));
if(module[2] && (retmodule = find_module(name, date, (int *)(module[2]))))
return retmodule;
else module=(int *)(module[4]);
}
while(module!=NULL);
return NULL;
}
void bf_link(int * this, int * that, unsigned short * thread)
{
PRINTVX(3, "Linking %s ", cstring(((char*)this)+0x30));
PRINTVX(3, "with %s\n", that ? cstring(((char*)that)+0x30) : "nil");
asm("pushal\n\
movl 0x10(%ebp),%ebx\n\
movl 0x08(%ebp),%esi\n\
movl 0x0C(%ebp),%ebp\n\
movw (%ebx),%ax\n\
shll $16,%eax\n\
movw 4(%ebx),%ax\n\
pushl %ebx\n\
movw 2(%ebx),%bx\n\
shll $16,%ebx\n\
call linkthread\n\
popl %ebx\n\
movw 6(%ebx),%ax\n\
movw 2(%ebx),%bx\n\
shll $16,%ebx\n\
call linkrel\n\
popal");
}
void linkit(char * nametable)
{
unsigned short tablesize;
int namesize;
int *this, *that;
char* endtable;
while(*((unsigned short *)nametable)) {
tablesize = *((unsigned short *)nametable);
nametable += sizeof(short);
PRINTVX(2, "Link module %s, table size %04x\n",cstring(nametable),tablesize);
endtable=nametable+tablesize;
namesize = ((int)*nametable)+1;
this=that=find_module(nametable, *(int *)(nametable + namesize), mroot);
PRINTVX(3, "Module %s found\n", cstring(((char*)this)+0x30));
nametable += namesize + sizeof(int);
bf_link(this, that, (unsigned short *)nametable);
nametable += 4*sizeof(short);
while(((unsigned int)nametable < (unsigned int)endtable) && that) {
namesize = 1 + *nametable;
that=find_module(nametable, *(int *)(nametable + namesize), mroot);
nametable += namesize;
if(that) nametable += sizeof(int);
bf_link(this, that, (unsigned short *)nametable);
nametable += 4*sizeof(short);
PRINTVX(4, "Table rest length %04x\n",(int)endtable - (int)nametable);
}
}
}
int * loadmod(int * freemem, OSFILE handle, int * modptr, long filepos)
{
int * module;
PRINTVX(3, "Loading to %08x from %08x module %08x pos %08lx\n",
(int)freemem, (int)handle, (int)modptr, filepos);
do {
if(filepos) seek_to(handle, filepos);
module=freemem+3;
read_bytes(handle, module, 8L);
if(module[1] == DEFAULTDICT) {
PRINTVX(4, "Replace dict size %x with %x\n", module[1], dict_);
module[1] = dict_;
}
PRINTVX(3, "Load Module with size %08x and DP %08x\n",module[1],module[0]);
freemem=bf_alloc(freemem, module[1] + ((module[1]-1) >> 3) +
1 + sizeof(int));
memset(((char *)module)+module[1], 0, ((module[1]-1)>>3) + 1);
module[-1]=1;
read_bytes(handle, module+2, module[0] - 2*sizeof(int));
PRINTV("Loaded module %s\n", cstring((char*)module+0x30));
read_bytes(handle, linkinfo, 2);
PRINTVX(3, "Loading link table %04x\n",*(unsigned short *)linkinfo);
read_bytes(handle, linkinfo+2, *(unsigned short *)linkinfo);
linkinfo += 2 + *(unsigned short *)linkinfo;
if(modptr == NULL) {
mroot=module;
} else {
int* parent = modptr-2;
while(parent[4])
parent=(int *)parent[4];
parent[4]=(int)module;
module[3]=(int)modptr;
}
{
int submodule=module[2];
module[2]=0;
if(submodule)
freemem=loadmod(freemem, handle, module, submodule);
}
filepos=module[4];
PRINTVX(4, "Loading next module at %08lx to %08lx\n", filepos, (long) freemem);
module[4]=0;
} while(filepos);
return freemem;
}
/* This loader is used for OSes with dynamic link libraries (such as
Windows, OS/2 or Linux), and therefore it presents a wrapper for
DLL loading
The wrapper runs over a table containing the following functionality:
- LoadModule
- GetProcAddr
for standard module loading
- type
- getkey
- at
- at?
- form
- bye
for kernal basic IO
and optionally
- set_recovery
for basic exception recovery.
*/
#ifdef OS2 /* Create OS/2-specific wrapper */
long bf_getkey(flag)
{
KBDKEYINFO inRecord;
KbdCharIn(&inRecord, flag ? 0 : 1, 0); /* Read Key, but don't wait */
if(inRecord.fbStatus)
return ((inRecord.chChar) | (inRecord.chScan << 8) |
(inRecord.fsState << 16));
else
return 0L;
}
void bf_type(long length, char * addr)
{
long dummy;
DosWrite(0, addr, length, &dummy);
}
long bf_at_query()
{
unsigned short row, col;
VioGetCurPos (&row, &col, 0);
return ((long)row << 16) | (long)col;
}
static long form=0;
long bf_form()
{
VIOMODEINFO mode;
if(form == 0)
{
if(VioGetMode (&mode, 0))
{
mode.row=25; mode.col=80;
}
form = (mode.row << 16) | (long)mode.col;
}
return form;
}
void bf_at(int col, int row)
{
VioSetCurPos (row, col, 0);
}
long bf_get_library(int length, char * addr)
{
char name[length+1];
HMODULE lib;
int rc;
memcpy(name, addr, length);
name[length]=0;
rc=DosLoadModule("", 0, name, &lib);
return (rc ? 0 : lib);
}
long bf_proc_addr(HMODULE lib, int length, char * addr)
{
char name[length+1];
PFN proc;
int rc;
memcpy(name, addr, length);
name[length]=0;
rc=DosQueryProcAddr (lib, 0, name, &proc);
return (long)(rc ? 0 : proc);
}
void prep_terminal()
{
KBDINFO kbdinfo;
KbdGetStatus(&kbdinfo, 0);
kbdinfo.fsMask = (kbdinfo.fsMask & 0xFFF0) | 6; /* noecho, raw mode */
KbdSetStatus(&kbdinfo, 0);
(void)bf_form();
}
void deprep_terminal()
{
KBDINFO kbdinfo;
KbdGetStatus(&kbdinfo, 0);
kbdinfo.fsMask = (kbdinfo.fsMask & 0xFFF0) | 0xA; /* noecho, cooked mode */
KbdSetStatus(&kbdinfo, 0);
}
void bf_bye(int ret)
{
deprep_terminal();
exit(ret);
}
void install_signal_handlers()
{
/* dummy */
}
#endif /* OS/2 specific wrapper */
#ifdef unix
#include <dlfcn.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#ifndef VMIN
#include <termios.h>
#endif
extern int errno;
static struct termios otio;
int readline_echoing_p = 1;
#ifndef CTRL
#define CTRL(key) ((key)^'@')
#endif
static int eof_char = CTRL('D');
static int terminal_prepped = 0;
static unsigned long cout=0, maxcur=0;
void get_winsize()
{
unsigned short rows=0, cols=0;
#ifdef TIOCGWINSZ
struct winsize size;
if (ioctl (1, TIOCGWINSZ, (char *) &size) >= 0) {
rows = size.ws_row;
cols = size.ws_col;
}
#endif
if((rows == 0) || (cols == 0)) {
char *s;
if ((s=getenv("LINES"))) {
rows=atoi(s);
}
if ((s=getenv("COLUMNS"))) {
cols=atoi(s);
}
}
if (rows==0) rows=24;
if (cols==0) cols=80;
maxcur = ((long)rows << 16) | (long)cols;
}
static void change_winsize(int sig, siginfo_t *info, void *_)
{
// signal(sig,change_winsize);
#ifdef TIOCGWINSZ
get_winsize();
#endif
}
void prep_terminal();
long get_screenpos()
{
unsigned short row=0, col=0;
char inchar;
if(!terminal_prepped) prep_terminal();
write(fileno(stdin), "\033[6n", 4);
while(read(fileno(stdin), &inchar, 1) && inchar !='R')
{
switch(inchar)
{
case '\033': break;
case '[': break;
case ';': row=col; col=0; break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': col=col*10+inchar-'0'; break;
default: /* something went wrong */ break;
}
}
row--; col--;
return ((long)row << 16) | (long)col;
}
void prep_terminal ()
{
int tty = fileno (stdin);
struct termios tio;
sigset_t set, oset;
if (terminal_prepped)
return;
setlocale(LC_ALL, "");
sigemptyset (&set);
sigemptyset (&oset);
sigaddset (&set, SIGINT);
sigprocmask (SIG_BLOCK, &set, &oset);
tcgetattr (tty, &tio);
otio = tio;
readline_echoing_p = (tio.c_lflag & ECHO);
tio.c_lflag &= ~(ICANON | ECHO);
if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
eof_char = otio.c_cc[VEOF];
if ((tio.c_cflag & CSIZE) == CS8)
tio.c_iflag &= ~(ISTRIP | INPCK);
tio.c_iflag &= ~(ICRNL | INLCR);
tio.c_lflag |= ISIG;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
tcsetattr (tty, TCSADRAIN, &tio);
tcflow (tty, TCOON);
terminal_prepped = 1;
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
#ifdef CLEAR_SCREEN
write(fileno(stdin), "\033[2J\033[H", 7);
cout = 0;
#else
if(isatty(tty)) {
cout = get_screenpos();
}
#endif
get_winsize();
}
void deprep_terminal ()
{
int tty = fileno (stdin);
sigset_t set, oset;
if (!terminal_prepped)
return;
sigemptyset (&set);
sigemptyset (&oset);
sigaddset (&set, SIGINT);
sigprocmask (SIG_BLOCK, &set, &oset);
tcsetattr (tty, TCSADRAIN, &otio);
tcflow (tty, TCOON);
terminal_prepped = 0;
sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
}
long pending = -1L;
long key_avail (stream)
FILE *stream;
{
int tty = fileno (stream);
long chars_avail = pending;
int result;
if(!terminal_prepped) prep_terminal();
result = ioctl (tty, FIONREAD, &chars_avail);
return chars_avail;
}
unsigned char getkey(stream)
FILE *stream;
{
int result;
unsigned char c;
if(!terminal_prepped) prep_terminal();
if (pending < 0)
{
result = read (fileno (stream), &c, sizeof (char));
if (result == sizeof (char))
return c;
if (errno != EINTR)
return (4);
if (result == 0)
return (0);
}
result = (int) pending;
pending = -1L;
return result;
}
long bf_getkey(int flag)
{
return((flag || key_avail(stdin)) ? (long)getkey(stdin) : 0L);
}
long bf_at_query()
{
if(!terminal_prepped) prep_terminal();
return cout;
}
long bf_form()
{
if(!maxcur) get_winsize();
return maxcur;
}
void bf_type(long length, char * addr)
{
int i;
write(fileno(stdout), addr, length);
for(i=0; i<length; i++)
switch(*addr++)
{
case '\b': cout--; break;
case '\t': cout+= 8; cout &= -8; break;
case '\n': cout >>= 0x10; cout++;
if(cout >= (maxcur >> 0x10))
cout = (maxcur >> 0x10) -1;
cout <<= 0x10; break;
default: cout++; break;
}
}
void bf_at(int col, int row)
{
char buf[20];
if(cout != (unsigned int)((row << 16) | col)) {
sprintf(buf,"\033[%d;%dH", row+1, col+1);
write(fileno(stdout), buf, strlen(buf));
cout = (row << 16) | col;
}
}
long bf_get_library(int length, char * addr)
{
char name[length+1];
memcpy(name, addr, length);
name[length]=0;
PRINTVX(2, "Open dynamic library %s\n", name);
return (long)dlopen(name, RTLD_LAZY | RTLD_GLOBAL); /* NOW */
}
long bf_proc_addr(long lib, int length, char * addr)
{
char name[length+1];
memcpy(name, addr, length);
name[length]=0;
PRINTVX(3, "Get symbol %s\n", name);
return (long)dlsym((void *)lib, name);
}
void bf_bye(int ret)
{
deprep_terminal();
exit(ret);
}
static void
graceful_exit (int sig, siginfo_t *info, void *_)
{
fprintf (stderr, "\n\n%s.\n", strsignal (sig));
deprep_terminal();
exit (0x80|sig);
}
#ifdef __FreeBSD__
#include <ucontext.h>
static void
signal_throw(int sig, siginfo_t *info, ucontext_t *uap)
{
int code;
mcontext_t *sigc = &(uap->uc_mcontext);
long *dump1, *dump2, *dump3;
struct {
int signal;
int throwcode;
} *p, throwtable[] = {
{ SIGINT, -28 },
/* { SIGFPE, -55 }, */
{ SIGBUS, -23 },
{ SIGSEGV, -9 },
};
dump1 = (long*)&(sigc->mc_edi);
dump2 = ((long*)(recovery))+1;
dump3 = (long*)(sigc->mc_fpstate);
memcpy(dump2, dump1, 8*sizeof(long));
dump2[3] = sigc->mc_esp;
dump2 += 8;
*dump2++ = sigc->mc_eip;
*dump2++ = sigc->mc_cs;
*dump2++ = sigc->mc_eflags;
*dump2++ = sigc->mc_trapno;
if(dump3) {
memcpy(fpdump, (char*)(dump3), 108);
*dump2-- = (long)fpdump;
} else {
*dump2-- = (long)0;
}
for (code=-256-sig, p=throwtable; p<throwtable+(sizeof(throwtable)/sizeof(*p)); p++)
if (sig == p->signal) {
code = p->throwcode;
break;
}
#if 1
if(sig == SIGFPE) {
switch(info->si_code) {
#ifdef FPE_INTDIV
case FPE_INTDIV: code=-10; break; /* integer divide by zero */
#endif
#ifdef FPE_INTOVF
case FPE_INTOVF: code=-11; break; /* integer overflow */
#endif
case FPE_FLTDIV: code=-42; break; /* floating point divide by zero */
case FPE_FLTOVF: code=-43; break; /* floating point overflow */
case FPE_FLTUND: code=-54; break; /* floating point underflow */
case FPE_FLTRES: code=-41; break; /* floating point inexact result */
#if 0 /* defined by Unix95, but unnecessary */
case FPE_FLTINV: /* invalid floating point operation */
case FPE_FLTSUB: /* subscript out of range */
#endif
default: code=-55; break;
}
}
#endif
longjmp(throw_jmp_buf,code); /* or use siglongjmp ? */
}
#endif
#ifdef linux
static void
signal_throw(int sig, siginfo_t *info, void *_)
{
int code;
struct sigcontext * sigc = (struct sigcontext *)((int *)(info+1)+5);
long *dump1, *dump2, *dump3;
struct {
int signal;
int throwcode;