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
|
use fmt;
use log;
use os;
use net;
use ev;
use unix::signal;
use io;
use memio;
use time::date;
use encoding::utf8;
use strings;
use bytes;
use shlex;
use time;
use getopt;
use dirs;
use path;
type server = struct {
status: *status,
loop: *ev::loop,
sock: *ev::file,
clients: []*client,
socketpath: str,
exit: int,
};
type client = struct {
server: *server,
sock: *ev::file,
buf: [os::BUFSZ]u8,
wbuf: []u8,
};
type status = struct {
clock: clock,
host: hostname,
generics: []generic,
};
type section = struct {
label: str,
};
type hostname = struct {
section: section,
value: str,
};
export fn main() void = {
const cmd = getopt::parse(os::args,
"status daemon",
('s', "path", "unix socket location")
);
defer getopt::finish(&cmd);
let socketpath = path::string(&path::init(dirs::runtime()!, "statrepsocket")!);
log::println(socketpath);
for (let opt .. cmd.opts) {
switch (opt.0) {
case 's' =>
socketpath = opt.1;
case =>
abort();
};
};
const loop = ev::newloop()!;
defer ev::finish(&loop);
const sock = ev::listen_unix(&loop, socketpath)!;
defer ev::close(sock);
let status = newstatus()!;
defer finishstatus(&status);
let state = server {
status = &status,
loop = &loop,
sock = sock,
socketpath = socketpath,
...
};
const clocktimer = ev::newtimer(&loop, &clocktimerf, time::clock::MONOTONIC)!;
ev::timer_configure(clocktimer, 1 * time::SECOND, 1 * time::SECOND);
ev::setuser(clocktimer, &state);
ev::setuser(sock, &state);
ev::accept(sock, &accept);
const sig = ev::signal(&loop, &signal, signal::sig::INT,
signal::sig::TERM)!;
ev::setuser(sig, &state);
defer ev::close(sig);
for (ev::dispatch(&loop, -1)!) void;
os::exit(state.exit);
};
fn newstatus() (status | io::error | utf8::invalid) = {
return status {
clock = buildclock()?,
host = buildhostname(),
generics = alloc([
buildgeneric("/home/jhurst/.local/share/statusbar/vol.sh"),
]),
};
};
fn finishstatus(status: *status) void = {
finishclock(&status.clock);
for (let gen &.. status.generics) {
finishgeneric(gen);
};
free(status.generics);
};
fn buildhostname() hostname = {
return hostname {
section = section {
label = "hostname: ",
},
value = os::hostname(),
};
};
fn printstatus(state: *status) str = {
const s = getstatus(state);
defer free(s);
fmt::println(strings::trim(s))!;
return s;
};
// Must free return value
fn getstatus(state: *status) str = {
const st = memio::dynamic();
defer io::close(&st)!;
for (let gen .. state.generics) {
memio::concat(&st, gen.value)!;
memio::concat(&st, " | ")!;
};
memio::concat(&st,
state.host.section.label, state.host.value, " | ",
state.clock.section.label, state.clock.value, "\n"
)!;
//const s = strings::concat(
// state.generic.section.label, state.generic.value, " | ",
// state.host.section.label, state.host.value, " | ",
// state.clock.section.label, state.clock.value, "\n");
const s = strings::dup(memio::string(&st)!);
return s;
};
fn accept(sock: *ev::file, result: (*ev::file | net::error)) void = {
let server = ev::getuser(sock): *server;
const sock = match (result) {
case let sock: *ev::file =>
yield sock;
case let err: net::error =>
log::printfln("Error: accept: {}", net::strerror(err));
ev::stop(server.loop);
server.exit = 1;
return;
};
log::println("accepted");
const client = alloc(client {
server = server,
sock = sock,
...
});
append(server.clients, client);
ev::setuser(client.sock, client);
ev::read(client.sock, &read, client.buf);
ev::accept(server.sock, &accept);
};
fn execmd(s: *status, cmd: []str) (void | str) = {
switch (cmd[0]) {
case "print" =>
printstatus(s);
case "status" =>
return getstatus(s);
case "clock" =>
updateclock(&s.clock)!;
case "generic" =>
for (let gen &.. s.generics) {
updategeneric(gen)!;
};
case =>
assert(false);
};
};
fn write(file: *ev::file, result: (size | io::error)) void = {
const client = ev::getuser(file): *client;
const n = match (result) {
case let err: io::error =>
log::printfln("Error: write: {}",
io::strerror(err));
client_close(client);
return;
case let n: size =>
yield n;
};
static delete(client.wbuf[..n]);
if (len(client.wbuf) != 0) {
ev::write(client.sock, &write, client.wbuf);
} else {
ev::read(client.sock, &read, client.buf);
};
};
fn read(file: *ev::file, result: (size | io::EOF | io::error)) void = {
const client = ev::getuser(file): *client;
const n = match (result) {
case let err: io::error =>
log::printfln("Error: read: {}",
io::strerror(err));
client_close(client);
return;
case io::EOF =>
client_close(client);
return;
case let n: size =>
yield n;
};
const b = client.buf[..n];
if (!bytes::contains(b, strings::toutf8("\n"))) {
ev::read(client.sock, &read, client.buf);
} else {
const s = match (strings::fromutf8(b)) {
case let s: str =>
yield s;
case let e: utf8::invalid =>
log::printfln("Error: read: {}", utf8::strerror(e));
client_close(client);
return;
};
const i = strings::index(s, "\n") as size;
const cmd = strings::sub(s, 0z, i);
log::println(cmd);
const spl = match (shlex::split(cmd)) {
case let e: shlex::syntaxerr =>
log::printfln("Error: read: {}", shlex::strerror(e));
client_close(client);
return;
case let spl: []str =>
yield spl;
};
defer strings::freeall(spl);
match (execmd(client.server.status, spl)) {
case let s: str =>
defer free(s);
log::println(strings::trim(s));
append(client.wbuf, strings::toutf8(s)...);
case =>
void;
};
if (len(client.wbuf) != 0) {
ev::write(client.sock, &write, client.wbuf);
} else {
ev::read(client.sock, &read, client.buf);
};
};
};
fn client_close(client: *client) void = {
const server = client.server;
for (let i = 0z; i < len(server.clients); i += 1) {
if (server.clients[i] == client) {
delete(server.clients[i]);
break;
};
};
log::println("Connection closed");
ev::close(client.sock);
free(client);
};
fn signal(file: *ev::file, sig: signal::sig) void = {
let server = ev::getuser(file): *server;
log::printfln("Exiting due to {}", signal::signame(sig));
const l = ev::getloop(file);
ev::stop(l);
os::remove(server.socketpath)!;
};
|