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
|
//! rd - Read unit contents
const colors = @import("colors");
const format = @import("format");
const io = @import("io");
const params = @import("params");
const sys = @import("sys");
var file_buffer: [64 * 1024]u8 = undefined;
export fn main(pc: u32, pv: [*]const [*:0]const u8) u8 {
const p = params.parse(pc, pv) catch |err| {
format.color("rd: ", colors.red);
format.println(@errorName(err));
return 1;
};
if (p.positionals.len == 0) {
format.colorln("rd: missing unit location.", colors.red);
return 1;
}
if (p.positionals.len > 1) {
format.colorln("rd: invalid number of positional parameters.", colors.red);
return 1;
}
if (p.named.len > 0) {
format.colorln("rd: named parameters are not supported.", colors.red);
return 1;
}
const location = switch (p.positional(0).?) {
.scalar => |s| s,
.list => {
format.colorln("rd: only one location allowed", colors.red);
return 1;
},
};
const fd = io.attach(location, io.VIEW_ONLY) catch {
format.color("rd: cannot access '", colors.red);
format.print(location);
format.colorln("': No such unit.", colors.red);
return 1;
};
const bytes_read = io.view(fd, &file_buffer) catch {
format.color("rd: cannot read '", colors.red);
format.print(location);
format.colorln("'.", colors.red);
io.seal(fd);
return 1;
};
if (bytes_read > 0) {
format.print(file_buffer[0..bytes_read]);
if (file_buffer[bytes_read - 1] != '\n') {
format.print("\n");
}
}
io.seal(fd);
return 0;
}
|