-
Notifications
You must be signed in to change notification settings - Fork 2
/
system.c
87 lines (80 loc) · 1.91 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
#include "c4.h"
#ifdef IS_WINDOWS
#include <conio.h>
int qKey() { return _kbhit(); }
int key() { return _getch(); }
void ttyMode(int isRaw) {}
#endif
#ifdef IS_LINUX
#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);
return x;
}
int key() {
ttyMode(1);
int x = fgetc(stdin);
return x;
}
#endif // IS_LINUX
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); }
// REP - Read/Execute/Print (no Loop)
void REP() {
char tib[128];
if (inputFp == 0) {
ttyMode(0);
ok();
}
if (fileGets(tib, sizeof(tib), inputFp)) {
outer(tib);
return;
}
if (inputFp == 0) { exit(0); }
fileClose(inputFp);
inputFp = filePop();
}
void loadArgument(const char *arg) {
char fn[32];
strCpy(fn, arg);
cell tmp = fileOpen(fn, "rb");
if (tmp) {
if (inputFp) { filePush(tmp); }
else { inputFp = tmp; }
}
}
int main(int argc, char *argv[]) {
c4Init();
if (argc > 1) { loadArgument(argv[1]); }
else { loadArgument("block-999.fth"); }
while (1) { REP(); };
return 0;
}