1 4d9326de 2018-12-01 omar.polo #!/usr/bin/env perl
3 4d9326de 2018-12-01 omar.polo use v5.10;
4 4d9326de 2018-12-01 omar.polo use warnings;
5 4d9326de 2018-12-01 omar.polo use IPC::Open3;
7 4d9326de 2018-12-01 omar.polo my $cachedir = $ENV{HOME}. "/.cache";
8 4d9326de 2018-12-01 omar.polo my $cachefile = $cachedir."/mymenu";
9 4d9326de 2018-12-01 omar.polo my %progs;
11 4d9326de 2018-12-01 omar.polo $SIG{CHLD} = "IGNORE";
13 4d9326de 2018-12-01 omar.polo if (! -d $cachedir) {
14 4d9326de 2018-12-01 omar.polo mkdir $cachedir or die "Cannot create cache dir ". $cachedir;
17 4d9326de 2018-12-01 omar.polo if (-e $cachefile) {
18 4d9326de 2018-12-01 omar.polo open my $f, "<", $cachefile;
19 4d9326de 2018-12-01 omar.polo while (<$f>) {
20 4d9326de 2018-12-01 omar.polo my ($c, $p) = split / /, $_;
21 4d9326de 2018-12-01 omar.polo chomp $p;
22 4d9326de 2018-12-01 omar.polo $progs{$p} = $c + 0;
24 4d9326de 2018-12-01 omar.polo close $f;
27 4d9326de 2018-12-01 omar.polo foreach my $p (split /:/, $ENV{PATH}) {
28 4d9326de 2018-12-01 omar.polo opendir my $d, $p or next;
29 4d9326de 2018-12-01 omar.polo foreach (readdir $d) {
30 4d9326de 2018-12-01 omar.polo if (-x "$p/$_" and ! (-d "$p/$_")) {
31 4d9326de 2018-12-01 omar.polo if (! exists $progs{$_}) {
32 4d9326de 2018-12-01 omar.polo $progs{$_} = 0;
36 4d9326de 2018-12-01 omar.polo closedir $d;
39 4d9326de 2018-12-01 omar.polo my $args = join ' ', @ARGV;
40 4d9326de 2018-12-01 omar.polo my $pid = open3(my $c_in, my $c_out, my $c_err, "mymenu $args") or die("Failed open3(): $!");
41 4d9326de 2018-12-01 omar.polo foreach (sort {$progs{$b} <=> $progs{$a} or lc($a) cmp lc($b)} keys %progs) {
42 4d9326de 2018-12-01 omar.polo print $c_in $_."\n";
44 4d9326de 2018-12-01 omar.polo close($c_in);
46 4d9326de 2018-12-01 omar.polo while (my $p = <$c_out>) {
47 4d9326de 2018-12-01 omar.polo chomp $p;
48 4d9326de 2018-12-01 omar.polo $progs{$p} += 1;
49 4d9326de 2018-12-01 omar.polo say "gonna exec $p";
51 4d9326de 2018-12-01 omar.polo if (!fork()) {
52 4d9326de 2018-12-01 omar.polo # i'm the child
53 4d9326de 2018-12-01 omar.polo exec $p;
57 4d9326de 2018-12-01 omar.polo say "Updating cache...";
58 4d9326de 2018-12-01 omar.polo open my $f, ">", $cachefile;
59 4d9326de 2018-12-01 omar.polo foreach (keys %progs) {
60 4d9326de 2018-12-01 omar.polo print $f $progs{$_} ." ". $_ ."\n" if $progs{$_} != 0;
62 4d9326de 2018-12-01 omar.polo close($f);
64 4d9326de 2018-12-01 omar.polo waitpid(-1, 0);