-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacos.jai
99 lines (81 loc) · 2.2 KB
/
macos.jai
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
// contains mac OS specific code.
// this is just copy/paste the linux code as macos.
EXE :: ""; // nothing.
NEWLINE :: "\n";
OS :: struct {}
// defines the AI uci message loop.
getline :: (os: *OS, loop_body: Code, flags: For_Flags) #expand {
nonblock_stdin :: () #expand {
flags := fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK;
fcntl(STDIN_FILENO, F_SETFL, flags);
}
nonblock_stdin();
`it_index := 0;
while outer := true {
stopping = false;
memset(buffera.data, 0, size_of(type_of(buffera)));
memset(bufferb.data, 0, size_of(type_of(bufferb)));
bytes_read := read(STDIN_FILENO, buffera.data, buffera.count-1);
if bytes_read < 0 {
sleep_milliseconds(25);
continue;
}
messages := to_string(buffera.data, bytes_read);
while messages {
found, `it, rest := split_from_left(messages, #char "\n");
if !found break;
#insert (break=break outer) loop_body;
messages = rest;
}
}
}
read_input :: (main_thread: bool, nodes: int, maxnodes: int, time_begin: float64, movetime: int) #expand {
if stopping == true then
`return 0;
if (nodes & 8191) == 8191 {
if nodes >= maxnodes {
stopping = true;
`return 0;
}
if main_thread == false
return;
if (nodes & 8191) == 8191 {
time := get_time();
left: int = xx (1000.0 * (time - time_begin));
if left > movetime {
stopping = true;
`return 0;
}
}
bytes_read := read(STDIN_FILENO, bufferb.data, bufferb.count-1);
if bytes_read > 0 {
str := to_string(bufferb.data, bytes_read-1); // -1 the '\n' char
while str {
found, msg, rest := split_from_left(str, #char "\n");
if equal(str, "isready") {
print("readyok\n");
}
if equal(str, "stop") {
stopping = true;
`return 0;
}
if equal(str, "quit") {
exit(0);
}
str = rest;
if !found break;
}
}
}
}
stop :: () -> bool #expand {
return stopping == true;
}
#scope_file
buffera: [4096] u8;
bufferb: [4096] u8;
stopping: bool = false;
#import "POSIX";
#import "Basic";
#import "String";
#import "System"; // For get_path_of_running_executable.