Blame
Date:
Sat Dec 1 15:43:21 2018 UTC
Message:
Added a couple of scripts
01
2018-12-01
omar.polo
#!/usr/bin/env perl
02
2018-12-01
omar.polo
03
2018-12-01
omar.polo
use v5.10;
04
2018-12-01
omar.polo
use warnings;
05
2018-12-01
omar.polo
use IPC::Open3;
06
2018-12-01
omar.polo
07
2018-12-01
omar.polo
my $cachedir = $ENV{HOME}. "/.cache";
08
2018-12-01
omar.polo
my $cachefile = $cachedir."/mymenu";
09
2018-12-01
omar.polo
my %progs;
10
2018-12-01
omar.polo
11
2018-12-01
omar.polo
$SIG{CHLD} = "IGNORE";
12
2018-12-01
omar.polo
13
2018-12-01
omar.polo
if (! -d $cachedir) {
14
2018-12-01
omar.polo
mkdir $cachedir or die "Cannot create cache dir ". $cachedir;
15
2018-12-01
omar.polo
}
16
2018-12-01
omar.polo
17
2018-12-01
omar.polo
if (-e $cachefile) {
18
2018-12-01
omar.polo
open my $f, "<", $cachefile;
19
2018-12-01
omar.polo
while (<$f>) {
20
2018-12-01
omar.polo
my ($c, $p) = split / /, $_;
21
2018-12-01
omar.polo
chomp $p;
22
2018-12-01
omar.polo
$progs{$p} = $c + 0;
23
2018-12-01
omar.polo
}
24
2018-12-01
omar.polo
close $f;
25
2018-12-01
omar.polo
}
26
2018-12-01
omar.polo
27
2018-12-01
omar.polo
foreach my $p (split /:/, $ENV{PATH}) {
28
2018-12-01
omar.polo
opendir my $d, $p or next;
29
2018-12-01
omar.polo
foreach (readdir $d) {
30
2018-12-01
omar.polo
if (-x "$p/$_" and ! (-d "$p/$_")) {
31
2018-12-01
omar.polo
if (! exists $progs{$_}) {
32
2018-12-01
omar.polo
$progs{$_} = 0;
33
2018-12-01
omar.polo
}
34
2018-12-01
omar.polo
}
35
2018-12-01
omar.polo
}
36
2018-12-01
omar.polo
closedir $d;
37
2018-12-01
omar.polo
}
38
2018-12-01
omar.polo
39
2018-12-01
omar.polo
my $args = join ' ', @ARGV;
40
2018-12-01
omar.polo
my $pid = open3(my $c_in, my $c_out, my $c_err, "mymenu $args") or die("Failed open3(): $!");
41
2018-12-01
omar.polo
foreach (sort {$progs{$b} <=> $progs{$a} or lc($a) cmp lc($b)} keys %progs) {
42
2018-12-01
omar.polo
print $c_in $_."\n";
43
2018-12-01
omar.polo
}
44
2018-12-01
omar.polo
close($c_in);
45
2018-12-01
omar.polo
46
2018-12-01
omar.polo
while (my $p = <$c_out>) {
47
2018-12-01
omar.polo
chomp $p;
48
2018-12-01
omar.polo
$progs{$p} += 1;
49
2018-12-01
omar.polo
say "gonna exec $p";
50
2018-12-01
omar.polo
51
2018-12-01
omar.polo
if (!fork()) {
52
2018-12-01
omar.polo
# i'm the child
53
2018-12-01
omar.polo
exec $p;
54
2018-12-01
omar.polo
}
55
2018-12-01
omar.polo
}
56
2018-12-01
omar.polo
57
2018-12-01
omar.polo
say "Updating cache...";
58
2018-12-01
omar.polo
open my $f, ">", $cachefile;
59
2018-12-01
omar.polo
foreach (keys %progs) {
60
2018-12-01
omar.polo
print $f $progs{$_} ." ". $_ ."\n" if $progs{$_} != 0;
61
2018-12-01
omar.polo
}
62
2018-12-01
omar.polo
close($f);
63
2018-12-01
omar.polo
64
2018-12-01
omar.polo
waitpid(-1, 0);
Omar Polo