#!/usr/bin/perl # DlMenu - an interactive menu system for an Xterm based on dialog # Copyright (C) 1999, Matthew Lavy # mml@mupsych.org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #--- CONFIGURATION SECTION --- # change the line below to point to your config file $CONF_FILE='/etc/dlmenu.conf'; # change the line below to name your menu $MENU_TITLE='---- Menu System ----'; # change the line below to point to dialog on your system $DIALOG='/usr/bin/dialog'; #--- END OF CONFIGURATION SECTION --- ############################################################################## # UNLESS MODIFYING THE PROGRAM, YOU SHOULD NOT NEED TO EDIT BELOW THIS LINE ############################################################################## sub readConfig() { unless (open(FILE,$CONF_FILE)){ print "Cannot open configuration file\n"; return; } my %menus; my $menu_name; while ($line = ){ chomp($line); next if $line =~/^[# ].*/; #it's a comment if($line =~/\[.*\]/){ #it's a section header $line =~ s/\[(.*)\]/$1/; $menu_name = $line; } else { #its a normal line my($title,$command)=split(/\s=\s/,$line); unless(! $title || ! $command) { push @{$menus{$menu_name}},[$title,$command]; } } } close FILE; return %menus; } sub menuLoop(@) { my @tags; my @MENU=@_; my $count = 1; my $item; foreach $item(@MENU){ push @tags, $count; push @tags, "\"$$item[0]\""; $count++; } my $stopShowing=0; while(! $stopShowing){ my $call = "$DIALOG_CMD --menu \"Choose an option\" 20 70 10 @tags"; my $random = rand 25000; # thanks to the Perl Cookbook for suggesting this # hideous bit of console redirection!! chomp(my $pressed=`$call 3>&1 1>&2 2>&3 3>&-`); if($? == 0){ # OK was pressed, so find out what to do my $execute=$MENU[$pressed-1][1]; # does it correspond to a key in the MENUS hashtable? if($MENUS{$execute}){ menuLoop(@{$MENUS{$execute}}); } else { system($execute); } } else { # cancel was pressed $stopShowing=1; } } } sub reallyQuit() { if(system("$DIALOG_CMD --yesno \"Are you sure you wish to exit?\" 10 45")){ return 0; # Do not quit } else { return 1; # Quit } } # START OF MAIN SCRIPT srand; $DIALOG_CMD="$DIALOG --title \"${MENU_TITLE}\""; %MENUS=readConfig(); if(! -x "/usr/bin/dialog"){ print "This program requires dialog to be present\n"; exit 1; } if(! %MENUS){ #Note: @MENUS{MAIN} always exists otherwise readConfig() returns null print "There is no menu to display. Adio!\n"; exit 2; } $finish=0; while(! $finish){ menuLoop(@{$MENUS{MAIN}}); if(reallyQuit()) { $finish=1; } } ############################################################################################################# # The format of the %MENUS structure # %menus = ( # MAIN => [["Check with Pine","pine"],["Change your password","passwd"]["Submenu","SUB1"]], # SUB1 => [["Find out where your cam mail is delivered","finger -l goose"],["Eat fishes","xeyes &"]] # ); ##############################################################################################################