-
Notifications
You must be signed in to change notification settings - Fork 0
/
9tail.myr
92 lines (69 loc) · 1.53 KB
/
9tail.myr
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
// A naive plan9 tail(1)-like implementation
use std
use bio
var follow // Watch and print further data
var nlines // Number of lines to print
var out // Buffered output
// Print usage information
const usage = {
std.fput(1, "usage: tail [+-number[lbr]] [-f] [ file… ]\n")
std.fatal("")
}
// Emit the lines of a file from a given line
const tail = {fd : std.fd
var file = bio.mkfile(fd, bio.Rd)
var nnl = 0
var nbytes = 0
const nl = ('\n' : byte)
var targl : int // Line to start printing from
// Clean up
bio.close(file)
std.close(fd)
bio.flush(out)
}
// Something vaguely tail(1) in Myrddin
const main = {argv : byte[:][:]
nlines = 10
follow = false
// Commandline argument processing
var i
for i = 1; i < argv.len; i++
match argv[i]
| "-h":
usage()
| "-f":
follow = true
| "--":
// Known end to arguments
i++
break
| _:
if (argv[i][0] : char) != ('-' : char) && (argv[i][0] : char) != ('+' : char)
// Found a non-flag argument
break
;;
// Handle -n[lbr] args
// line, byte, rune ; line is the default
;;
std.put("→ {}\n", argv[i])
;;
var args = argv[i:]
std.put("len = {}\n", args.len)
out = bio.mkfile((1 : std.fd), bio.Wr)
match args.len
| 0:
// Read from stdin if no [file] is specified
tail((0 : std.fd))
| _:
// For each [file …] argument
for file : args
match std.open(file, std.Oread)
| `std.Ok fd:
tail(fd)
| `std.Err err:
std.fatal("err: could not open {} - {}\n", file, err)
;;
;;
;;
bio.close(out)
}