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:
Got something to add? Send me email.
More Articles by Tony Lawrence © 2012-07-14 Tony Lawrence
You can't do it unless you can imagine it. (George Lucas)
Printer Friendly Version
KCMENU (Kevin Clark's menu generator) in Perl Copyright © April 2011 Tony Lawrence
Have you tried Searching this site?
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
Printer Friendly Version