-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.c
106 lines (93 loc) · 2.48 KB
/
system.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
// A ColorForth inspired system, MIT license
#include "cf.h"
#ifdef IS_WINDOWS
#include <windows.h>
#include <conio.h>
int qKey() { return _kbhit(); }
int key() { return _getch(); }
void ttyMode(int isRaw) {}
void ms(cell sleepForMS) { Sleep((DWORD)sleepForMS); }
#endif
// Support for Linux, OpenBSD, FreeBSD
#if defined(__linux__) || defined(__OpenBSD__) || defined(__FreeBSD__)
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
void ttyMode(int isRaw) {
static struct termios origt, rawt;
static int curMode = -1;
if (curMode == -1) {
curMode = 0;
tcgetattr( STDIN_FILENO, &origt);
cfmakeraw(&rawt);
}
if (isRaw != curMode) {
if (isRaw) {
tcsetattr( STDIN_FILENO, TCSANOW, &rawt);
} else {
tcsetattr( STDIN_FILENO, TCSANOW, &origt);
}
curMode = isRaw;
}
}
int qKey() {
struct timeval tv;
fd_set rdfs;
ttyMode(1);
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET(STDIN_FILENO, &rdfs);
select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
int x = FD_ISSET(STDIN_FILENO, &rdfs);
// ttyMode(0);
return x;
}
int key() {
ttyMode(1);
int x = fgetc(stdin);
// ttyMode(0);
return x;
}
void ms(cell sleepForMS) {
while (sleepForMS > 1000) {
usleep(500000);
usleep(500000);
sleepForMS -= 1000;
}
if (sleepForMS > 0) { usleep(sleepForMS * 1000); }
}
#endif // Linux, OpenBSD, FreeBSD
cell timer() { return (cell)clock(); }
void zType(const char* str) { fputs(str, outputFp ? (FILE*)outputFp : stdout); }
void emit(const char ch) { fputc(ch, outputFp ? (FILE*)outputFp : stdout); }
cell fOpen(const char *name, cell mode) { return (cell)fopen(name, (char*)mode); }
void fClose(cell fh) { fclose((FILE*)fh); }
cell fRead(cell buf, cell sz, cell fh) { return (cell)fread((char*)buf, 1, sz, (FILE*)fh); }
cell fWrite(cell buf, cell sz, cell fh) { return (cell)fwrite((char*)buf, 1, sz, (FILE*)fh); }
cell fSeek(cell fh, cell offset) { return (cell)fseek((FILE*)fh, (long)offset, SEEK_SET); }
void repl() {
char tib[256];
ttyMode(0);
zType((state == COMPILE) ? " ... " : " ok\n");
if (fgets(tib, 256, stdin) != tib) { exit(0); }
outer(tib);
}
void boot(const char *fn) {
if (!fn) { fn = "boot.cf"; }
cell fp = fOpen(fn, (cell)"rb");
if (fp) {
fRead((cell)&mem[100000], 99999, fp);
fClose(fp);
outer((char*)&mem[100000]);
} else {
zType("WARNING: unable to open source file!\n");
zType("If no filename is provided, the default is 'boot.cf'\n");
}
}
int main(int argc, char *argv[]) {
Init();
boot((1<argc) ? argv[1] : 0);
while (1) { repl(); }
return 0;
}