-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttygrep
executable file
·63 lines (54 loc) · 1.41 KB
/
ttygrep
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
#!/usr/bin/perl
# Search a ttyrec file for text matching a regexp. Print the frame
# number(s) in which it's found, so that ipbt can be used to go
# straight there.
@args = ();
$opts = 1;
$join = 0;
foreach $i (@ARGV) {
if ($opts and $i =~ /^-(.*)/) {
if ($1 eq "-") {
$opts = 0;
} elsif ($1 eq "j") {
$join = 1;
} else {
warn "unrecognised option '-$1'\n";
}
} else {
push @args, $i;
}
}
if (!defined $args[1]) {
print STDERR "usage: ttygrep <pattern> <file> [<file>...]\n";
# If called with no arguments, we assume we're being _asked_ to
# print the usage statement, so we do so and exit with no
# error. If called with one argument, we have no remaining
# choice but to assume the user is mistaken.
exit (defined $args[0]);
}
$pattern = shift @args;
$tframe = 0;
foreach $file (@args) {
open F, "<$file" or die "$0: unable to open '$file'\n";
$frame = 0;
while (1) {
$hdr = "";
$hgot = read F, $hdr, 12;
last if $hgot == 0; # clean EOF
die "$0: unexpected EOF in '$file' frame header\n" if $hgot < 12;
@hdrvals = unpack "VVV", $hdr;
$dlen = $hdrvals[2];
$data = "";
$dgot = read F, $data, $dlen;
die "$0: unexpected EOF in '$file' frame data\n" if $dgot < $dlen;
if ($join) {
$location = "$tframe";
} else {
$location = "$file:$frame";
}
print "$location:$&\n" if $data =~ /[ -~]*$pattern[ -~]*/s;
$frame++;
$tframe++;
}
close F;
}