Great Circle Associates Majordomo-Users
(September 1993)
 

Indexed By Date: [Previous] [Next] Indexed By Thread: [Previous] [Next]

Subject: Patch for command abbrev. and easy command add-in
From: Alan Millar <amillar @ bolis . sf-bay . org>
Date: Sun, 5 Sep 1993 03:21:12 -0800 (PDT)
To: brent @ GreatCircle . COM
Cc: majordomo-users @ GreatCircle . COM
Reply-to: Alan Millar <AMillar @ bolis . sf-bay . org>

Here is a patch to Majordomo that allows commands to be
abbreviated (such as "sub" and "unsub" for subscribe and
unsubscribe).  Abbreviations can be as short as you want,
except when an abbreviation would match more than one
valid command.  This patch is based on Majordomo v 1.60

In addition, the list of commands is now derived automatically
from the list of perl subroutines with a "do_xxx" name
(although you can override this behavior).  To add a new
command, all you have to do is create a subroutine named
do_xxxxx and xxxxx will automatically be recognized as
a valid command.

I'd appreciate any comments.  Thanks!

- Alan


*** majordomo.old	Fri Sep  3 01:23:05 1993
--- majordomo	Sun Sep  5 03:11:23 1993
***************
*** 72,77 ****
--- 72,139 ----
      }
  }
  
+ # Establish list of valid commands, and valid abbreviations.
+ # Assumption: no valid command is a subset of another valid
+ #   command.  For example, two imaginary commands "abcdefg" 
+ #   and "abc1234" are distinguishable, but another command "abc" 
+ #   cannot be recognized.
+ # No explicit list of commands is needed.  The list of commands 
+ #   is taken from the list of all subroutines named "do_xxx" in 
+ #   package "main".  If you want to add a command to Majordomo, 
+ #   just create a subroutine called "do_abc", and "abc" will 
+ #   automatically be recognized as a valid command.  Recommended
+ #   method:  put your subroutine in a file such as "local_stuff.pl",
+ #   and put in majordomo.cf:   require "$homedir/local_stuff.pl";
+ #   To bypass the do_xxx subroutine list, specify a list of
+ #   commands in the array "@commands".
+ # To set up a list of additional command aliases (such as
+ #   allowing the command "review" for "who" or "signoff"
+ #   for "unsubscribe") specify it as a list of pairs in
+ #   majordomo.cf like this:
+ #     %alternate_commands = ("review", "do_who",
+ #                            "signoff", "do_unsubscribe")
+ #   where the first word in the pair is the command name
+ #   and the second word is the subroutine to execute.
+ # To set a mininum command length, set $min_cmd_length 
+ #   in majordomo.cf.  For example, if you set $min_cmd_length
+ #   to 2, then "subscribe" will accept abbreviations as short as "su".
+ 
+ # Set up main list of commands.
+ if (!defined(@commands)) {
+     # An explicit list of commands has not been specified.
+     #   Have perl tell us which "do_xxxx" subroutines exist in main.
+     foreach $subroutine (keys(%_main)) {
+ 	if (defined(&$subroutine) && $subroutine =~ /^do_/) {
+ 	    ($cmd = $subroutine) =~ s/^do_//;
+ 	    $command_subs{$cmd} = $subroutine;
+ 	}
+     }
+ } else {
+     # Given explicit list of commands, check for subroutines 
+     foreach $cmd (@commands) {
+       $subroutine = "do_$cmd";
+       if (defined (&$subroutine)) { $command_subs{$cmd} = $subroutine; }
+     }
+ }
+ foreach $cmd (keys(%alternate_commands)) {
+   $subroutine = "$alternate_commands{$cmd}";
+   if (defined (&$subroutine)) { $command_subs{$cmd} = $subroutine; }
+ } 
+ if (!defined($min_cmd_length) || $min_cmd_length < 1) {
+   $min_cmd_length = 3;
+ }
+ foreach $cmd (keys(%command_subs)) {
+     # Now that we know our commands, shorten them.
+     for ($cmd_len = length($cmd) - 1; $cmd_len >= $min_cmd_length ;$cmd_len--) {
+ 	$short_cmd = substr($cmd,0,$cmd_len);
+ 	$ambiguous_count{$short_cmd}++;
+ 	if(!defined($command_subs{$short_cmd})) {
+ 	    $command_subs{$short_cmd} = $command_subs{$cmd};
+ 	    $long_cmd_name{$short_cmd} = $cmd;
+ 	}
+     }
+ }
+ 
  # set our hostname (for use in log messages).
  $hostname = &chop_nl(`hostname`);
  
***************
*** 117,134 ****
      $count++;	# assume it's a valid command, so count it.
      if ($cmd eq "end") { print REPLY "END OF COMMANDS\n"; last; }
      elsif ($cmd =~ /^-/) { print REPLY "END OF COMMANDS\n"; last; }
!     elsif ($cmd eq "subscribe") { &do_subscribe(@parts); }
!     elsif ($cmd eq "unsubscribe") { &do_unsubscribe(@parts); }
!     elsif ($cmd eq "approve") { &do_approve(@parts); }
!     elsif ($cmd eq "passwd") { &do_passwd(@parts); }
!     elsif ($cmd eq "which") { &do_which(@parts); }
!     elsif ($cmd eq "who") { &do_who(@parts); }
!     elsif ($cmd eq "info") { &do_info(@parts); }
!     elsif ($cmd eq "newinfo") { &do_newinfo(@parts); }
!     elsif ($cmd eq "lists") { &do_lists(@parts); }
!     elsif ($cmd eq "help") { &do_help(@parts); }
!     elsif ($cmd eq "get") { &do_get(@parts); }
!     elsif ($cmd eq "index") { &do_index(@parts); }
      else {
  	&squawk("Command '$cmd' not recognized.");
  	$count--;	# if we get to here, it wasn't really a command
--- 179,197 ----
      $count++;	# assume it's a valid command, so count it.
      if ($cmd eq "end") { print REPLY "END OF COMMANDS\n"; last; }
      elsif ($cmd =~ /^-/) { print REPLY "END OF COMMANDS\n"; last; }
!     elsif (defined($command_subs{$cmd})) { 
! 	if ($ambiguous_count{$cmd} <= 1) {
! 	    # It is a valid command.
! 	    #   If it is an abbrev., tell user what we interpret it as.
! 	    if($long_cmd_name{$cmd}) {print REPLY ">$cmd: $long_cmd_name{$cmd}\n";}
! 	    # Call the subroutine.
!             $subroutine = $command_subs{$cmd};
!             &$subroutine(@parts);
! 	} else {
! 	   &squawk("'$cmd' matches several commands.  " .
!                    "Please use the full command name.");
! 	}
!     }
      else {
  	&squawk("Command '$cmd' not recognized.");
  	$count--;	# if we get to here, it wasn't really a command



----                                                            ,,,, 
Alan Millar            amillar@bolis.SF-Bay.org              __oo  \  
System Administrator                                           =___/
Reason notwithstanding, the universe continues unabated.



Follow-Ups:
Indexed By Date Previous: "approve" doesn't read stdin? (+ FIX)
From: Jerry Peek <jerry@ora.com>
Next: Book about Majordomo
From: Jerry Peek <jerry@ora.com>
Indexed By Thread Previous: "approve" doesn't read stdin? (+ FIX)
From: Jerry Peek <jerry@ora.com>
Next: Re: Patch for command abbrev. and easy command add-in
From: "John P. Rouillard" <rouilj@cs.umb.edu>

Google
 
Search Internet Search www.greatcircle.com