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/", which it will try to "exec". If that works, you get your menu. If not, examine "/menus/".

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.

$ENV{PATH} = ".:$ENV{PATH}";
$file=shift @ARGV;
open(I,$file) or $notfound=1;
if ($notfound) {
open(I,$file) or $notfound=1;
if ($notfound) {
 print "Can't find $ofile\n";
 exit 1;

while(<I>) {
  next if /^#/;
  next if /^$/;
  if (not $title) {
    $title=~ s/^..//; 
    chomp $title;
  $delim=substr $_,0,1;
  next if ((substr $_,1,1) eq $delim);
  ($j,$s[$x],$p[$x],$e[$x])= split /$delim/;
  $x++ ;
  $width=$l if ($l >$width );
$width=$width % 80;
$file .= "";
print O <<EOF;
use Term::ANSIMenu;
my \$menu = Term::ANSIMenu->new(
                               width  => $width,
                               title  => '$title',
print O "help   => [['', \\&standard_help],\n";
foreach(@p) {
  print O qq([ '$_', undef ],\n);
print O "],\n";
print O "items  => [";
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);
print O <<EOF;
                               status => '$p[0]',
                               prompt => 'Please choose: ');
  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);
close O;

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?

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