Perl Kcmenu

Many years ago Kevin Clark wrote a nice little menu generator for SCO systems. I wrote about it at Kcmenu.

Unfortunately, Kevin has misplaced the source code so it is constrained to SCO Unix systems. As most of those systems are converting to Linux (see the many "Conversion" articles here), that's unfortunate.

I thought about re-writing this in Perl. As I researched how I might do that, I came across Term::ANSIMenu.

Normally I hate using anyone's code, but I thought this might save a lot of time.

What this will do is do a quick, dirty translation of "kcmenu" menu files into Perl. It's not perfect, but as it produces Perl code, you could always modify that code as needed.

I haven't done any excessive testing. I wrote this in all of twenty minutes this morning, so it could easily blow up. I know it does work with the sample menu provided at that original article.

When run, it creates a Perl executable using the name of the menu file as a template and attempts to "exec" that file. for example, if you named the Perl script "kcmenu", and did:

kcmenu /menus/sample
 

It would look for a menu file named "/menus/sample", read it, and create a new Perl script called "/menus/samplekc.pl", which it will try to "exec". If that works, you get your menu. If not, examine "/menus/samplekc.pl".

You'll need to get Term::ANSIMenu from CPAN, of course. If you've used CPAN before, just fire it up and do "install Term::ANSIMenu".

Here's the code. If you run into issues, send me the menu file and I'll try to accomodate you. If you spot bugs, please send back modified codes.

This doesn't support the original "exit" function. It could, but I didn't take the time to code it so "Q" or "q" quits regardless of what your "exit" line says. It also just ignores lines that are not complete menu items; Kevin's code would print them.

#!/usr/bin/perl
$ENV{PATH} = ".:$ENV{PATH}";
$file=shift @ARGV;
$ofile=$file;
$width=40;
open(I,$file) or $notfound=1;
if ($notfound) {
$notfound=0;
$file="/usr/kctools/$file";
open(I,$file) or $notfound=1;
}
if ($notfound) {
 print "Can't find $ofile\n";
 exit 1;
}

$x=0;
while(<I>) {
  next if /^#/;
  next if /^$/;
  if (not $title) {
    $title=$_;
    $title=~ s/^..//; 
    chomp $title;
    next;
   }
  $delim=substr $_,0,1;
  next if ((substr $_,1,1) eq $delim);
  print;
  chomp;
  ($j,$s[$x],$p[$x],$e[$x])= split /$delim/;
  $l=length($p[$x]);
  $x++ ;
  $width=$l if ($l >$width );
}
$width=$width % 80;
$file .= "kc.pl";
open(O,">$file");
print O <<EOF;
#!/usr/bin/perl
use Term::ANSIMenu;
my \$menu = Term::ANSIMenu->new(
                               width  => $width,
                               title  => '$title',
EOF
print O "help   => [['', \\&standard_help],\n";
foreach(@p) {
  print O qq([ '$_', undef ],\n);
}
print O "],\n";
print O "items  => [";
$x=0;
foreach(@s) {
  $s=substr $s[$x],0,1;
  if ($s[$x] and not $e[$x]) { 
    print O qq([ '$s', '$s[$x]',undef ],\n);
  }
  if ($s[$x] and $e[$x]) {
    print O qq([ '$s','$s[$x]',sub {system "$e[$x]"} ],\n);
  }
  $x++;
}
print O <<EOF;
],
                               status => '$p[0]',
                               prompt => 'Please choose: ');
\$menu->print_menu();
  while (my \$key = \$menu->read_key()) {
    last unless defined \$menu->do_key(\$key);
    \$menu->update_status('') if \$key eq 'S';
    \$menu->update_status('New status') if \$key eq 's';
    \$menu->update_prompt('') if \$key eq 'P';
    \$menu->update_prompt('New prompt: ') if \$key eq 'p';
  }
  \$menu->pos(\$menu->line_after_menu() + 1, 1);
EOF
close O;
chmod(0755,$file);
exec($file);

Here's what it looks like running the sample menu:

Perl kcmenu example

Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> KCMENU- Unix Menu Program (Perl)




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





The errors which arise from the absence of facts are far more numerous and more durable than those which result from unsound reasoning respecting true data. (Charles Babbage)

A crowded police docket is the surest of all signs that trade is brisk and money plenty. (Mark Twain)








This post tagged: