-
Notifications
You must be signed in to change notification settings - Fork 5
/
sys-pc.cpp
176 lines (158 loc) · 4.69 KB
/
sys-pc.cpp
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
// Support for PCs
#include "c3.h"
#ifdef isPC
enum { SYSTEM = 100, FOPEN, FCLOSE, FREAD, FWRITE, FGETS, FLOAD, BLOAD,
EDIT_BLK
};
#ifdef IS_WINDOWS
#include <time.h>
#include <conio.h>
int qKey() { return _kbhit(); }
int key() { return _getch(); }
#elif (defined IS_LINUX)
#include <time.h>
#include <unistd.h>
#include <termios.h>
static struct termios normT, rawT;
static int isTtyInit = 0;
void ttyInit() {
tcgetattr(STDIN_FILENO, &normT);
cfmakeraw(&rawT);
isTtyInit = 1;
}
void ttyModeNorm() {
if (!isTtyInit) { ttyInit(); }
tcsetattr(STDIN_FILENO, TCSANOW, &normT);
}
void ttyModeRaw() {
if (!isTtyInit) { ttyInit(); }
tcsetattr(STDIN_FILENO, TCSANOW, &rawT);
}
int qKey() {
struct timeval tv;
fd_set rdfs;
ttyModeRaw();
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);
ttyModeNorm();
return x;
}
int key() {
ttyModeRaw();
int x = getchar();
ttyModeNorm();
return x;
}
#endif // IS_LINUX
void printChar(const char c) { fputc(c, output_fp ? (FILE*)output_fp : stdout); }
void printString(const char* s) { fputs(s, output_fp ? (FILE*)output_fp : stdout); }
cell_t sysTime() { return clock(); }
void Store(char *loc, cell_t x) { *(cell_t*)loc = x; }
cell_t Fetch(const char *loc) { return *(cell_t*)loc; }
char *root;
void getInput() {
in = tib;
fill(tib, 0, sizeof(tib));
if ((state == STOP_LOAD) && input_fp) {
fClose(input_fp);
input_fp = ipop();
state = 0;
return;
}
if (input_fp) {
in = fgets(tib, 190, (FILE*)input_fp);
if (in != tib) {
fClose(input_fp);
input_fp = ipop();
}
}
if (! input_fp) {
cell_t tmp = output_fp;
output_fp = 0;
printString((state) ? "... > " : " ok\n");
in = fgets(tib, sizeof(tib), stdin);
output_fp = tmp;
}
}
int tryOpen(const char *root, const char *loc, const char *fn) {
char nm[64];
strCpy(nm, root);
strCat(nm, loc);
strCat(nm, fn);
// printf("try [%s]\n", nm);
cell_t fp = fOpen(nm, "rb");
if (!fp) { return 0; }
if (input_fp) { ipush(input_fp); }
input_fp = fp;
return 1;
}
int lookForFile(const char *name) {
#ifdef IS_LINUX
if (tryOpen("", "", name)) { return 1; }
if (tryOpen("", "./", name)) { return 1; }
if (tryOpen(root, "/c3/", name)) { return 1; }
if (tryOpen(root, "/bin/c3/", name)) { return 1; }
if (tryOpen(root, "/bin/", name)) { return 1; }
#elif (defined IS_WINDOWS)
if (tryOpen("", "", name)) { return 1; }
if (tryOpen("", ".\\", name)) { return 1; }
if (tryOpen(root, "\\c3\\", name)) { return 1; }
if (tryOpen(root, "\\bin\\c3\\", name)) { return 1; }
if (tryOpen(root, "\\bin\\", name)) { return 1; }
#endif
return 0;
}
void LFF(char *fn) { if (!lookForFile(fn)) { printStringF("-file[%s]?-", fn); } }
char *doUser(char *pc, int ir) {
cell_t t1, t2, t3;
char fn[16];
switch (ir) {
case SYSTEM: system(cpop());
RCASE FOPEN: t2=pop(); t1=pop(); push(fOpen((char*)t1, (char*)t2));
RCASE FCLOSE: fClose(pop());
RCASE FREAD: t3=pop(); t2=pop(); t1=pop(); push(fRead((char*)t1, 1, (int)t2, t3));
RCASE FWRITE: t3=pop(); t2=pop(); t1=pop(); push(fWrite((char*)t1, 1, (int)t2, t3));
RCASE FGETS: t3=pop(); t2=pop(); t1=pop(); push(fGets(t3, (char*)t1, (int)t2));
RCASE FLOAD: LFF(cpop());
RCASE BLOAD: sprintf(fn, "block-%03d.fth", (int)pop()); lookForFile(fn);
RCASE EDIT_BLK: t1=pop(); editBlock(t1);
return pc; default: return 0;
}
}
void loadUserWords() {
parseF("-ML- SYSTEM %d 3 -MLX- inline", SYSTEM);
parseF("-ML- FOPEN %d 3 -MLX- inline", FOPEN);
parseF("-ML- FCLOSE %d 3 -MLX- inline", FCLOSE);
parseF("-ML- FREAD %d 3 -MLX- inline", FREAD);
parseF("-ML- FWRITE %d 3 -MLX- inline", FWRITE);
parseF("-ML- FGETS %d 3 -MLX- inline", FGETS);
parseF("-ML- (LOAD) %d 3 -MLX- inline", FLOAD);
parseF("-ML- LOAD %d 3 -MLX- inline", BLOAD);
parseF("-ML- EDIT %d 3 -MLX- inline", EDIT_BLK);
ParseLine(": isPC 1 ;");
parseF(": BYE %d STATE ! ;", ALL_DONE);
}
int main(int argc, char *argv[]) {
root=(char*)"";
input_fp = output_fp = 0;
c3Init();
if (1 < argc) { root = argv[1]; }
for (int i=2; i<argc; i++) {
if (lookForFile(argv[i])) { continue; }
if (isNum(argv[i])) { reg[i] = pop(); }
else { reg[i] = (cell_t)argv[i]; }
}
#ifndef _SYS_LOAD_
lookForFile("block-001.fth");
#endif
while (state != ALL_DONE) {
getInput();
ParseLine(tib);
}
return 0;
}
#endif // isPC