| 1 | package PerlConsole::Commands; | ||
| 2 | # Copyright © 2007 Alexis Sukrieh | ||
| 3 | # | ||
| 4 | # This program is free software; you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU General Public License as published by the Free Software | ||
| 6 | # Foundation; either version 2 of the License, or (at your option) any later | ||
| 7 | # version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along with | ||
| 15 | # this program; if not, write to the Free Software Foundation, Inc., 51 | ||
| 16 | # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | # All the internal commands of the Perl Console are defined here. | ||
| 22 | |||
| 23 | my $help = { | ||
| 24 | preferences => "Display all the avaialbe preferences and how to change them.", | ||
| 25 | quit => "Quit the console.", | ||
| 26 | }; | ||
| 27 | |||
| 28 | # display the help message | ||
| 29 | |||
| 30 | # returns 1 if the string is an internal command | ||
| 31 | sub isInternalCommand($$) | ||
| 32 | { | ||
| 33 | my ($class, $code) = @_; | ||
| 34 | return 0 unless $code; | ||
| 35 | chomp($code); | ||
| 36 | return $code =~ /^\s*:(\S+)\s*/; | ||
| 37 | } | ||
| 38 | |||
| 39 | # Execute the internal command given | ||
| 40 | sub execute($$$) | ||
| 41 | { | ||
| 42 | my ($class, $console, $code) = @_; | ||
| 43 | |||
| 44 | # preference : output | ||
| 45 | if ($code =~ /^\s*:set/) { | ||
| 46 | if ($code =~ /^\s*:set\s+(\S+)\s*=\s*(\S+)/) { | ||
| 47 | my ($pref, $val) = ($1, $2); | ||
| 48 | if ($pref eq "output") { | ||
| 49 | $console->setOutput($val); | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | $console->setPreference($pref, $val); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $console->error("invalid syntax for setting a preference, see :help preferences"); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # The main help page | ||
| 61 | elsif ($code =~ /^\s*:help\s*$/) { | ||
| 62 | $console->message(PerlConsole::Commands->help($console)); | ||
| 63 | } | ||
| 64 | |||
| 65 | # The help page of a specified topic | ||
| 66 | elsif ($code =~ /^\s*:help\s+(\S+)/) { | ||
| 67 | $console->message(PerlConsole::Commands->help($console, $1)); | ||
| 68 | } | ||
| 69 | |||
| 70 | # display the logs stack | ||
| 71 | elsif ($code =~ /^\s*:logs/) { | ||
| 72 | foreach my $log (@{$console->getLogs}) { | ||
| 73 | $console->message($log); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | # at this point, unrecognized command | ||
| 78 | else { | ||
| 79 | $console->error("no such command"); | ||
| 80 | } | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Returns an help message, on a topic | ||
| 85 | sub help | ||
| 86 | { | ||
| 87 | my ($class, $console, $topic) = @_; | ||
| 88 | if (! defined $topic) { | ||
| 89 | return "The following help topics are available:\n". | ||
| 90 | join("\n- ", keys%{$help}); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | # preferences have automated online help | ||
| 94 | if ($topic =~ /preferences/) { | ||
| 95 | return $console->{'prefs'}->help(); | ||
| 96 | } | ||
| 97 | elsif (grep /^$topic$/, $console->{'prefs'}->getPreferences()) { | ||
| 98 | return $console->{'prefs'}->help($topic); | ||
| 99 | } | ||
| 100 | elsif (defined $help->{$topic}) { | ||
| 101 | return $help->{$topic}; | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | return "No such help topic: $topic"; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | # END | ||
| 110 | 1; |