APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version


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
If this page was useful to you, please help others find it:  





Comments?




More Articles by Tony Lawrence - Find me on Google+



Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Conversion
       - Perl
       - Programming
       - Scripting


















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals