Changeset 176

Show
Ignore:
Timestamp:
07/07/04 18:44:42 (4 years ago)
Author:
athomas
Message:

Fixed a fairly major bug where VALIDATE completely stopped wildcard targets
from working.

Added validation to the configuration command set. This enforces
validation of keys and values during parsing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • manage/trunk/CLI.pm

    r175 r176  
    263263                        my $type = ref($cmd->{$key}); 
    264264 
     265                                if ($type eq 'HASH' and $cmd->{$key}->{VALIDATE}) { 
     266                                my $type = ref($cmd->{$key}->{VALIDATE}); 
     267 
     268                                        if (not $type) { 
     269                                                if ($cmd->{$key}->{VALIDATE} and $cmd->{$key}->{HELP}) { 
     270                                                my %commands; 
     271 
     272                                                        $process_help->($cmd->{$key}->{HELP}, \%commands, '<eol>'); 
     273                                                        return ('ERROR', "Invalid input '$token'.", $ctx) 
     274                                                                unless $commands{$token}; 
     275                                                } 
     276                                        } elsif ($type eq 'CODE') { 
     277                                                return ('ERROR', "Invalid input '$token'.", $ctx) 
     278                                                        unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()}); 
     279                                        } elsif ($type eq 'HASH') { 
     280                                                return ('ERROR', "Invalid input '$token'.", $ctx) 
     281                                                        unless $cmd->{$key}->{VALIDATE}->{$token}; 
     282                                        } elsif ($type eq 'ARRAY') { 
     283                                        my $found = 0; 
     284 
     285                                                VALIDATE_ARRAY: for my $t (@{$cmd->{$key}->{VALIDATE}}) { 
     286                                                        $found =1, last VALIDATE_ARRAY 
     287                                                                if $token eq $t; 
     288                                                } 
     289                                                return ('ERROR', "Invalid input '$token'.", $ctx) 
     290                                                        unless $found; 
     291                                        } else { 
     292                                                return ('ERROR', 'Invalid value for VALIDATE target.', $ctx); 
     293                                        } 
     294                                } 
     295 
    265296                                $ctx->next_arg($token) if $trackvar; 
    266297 
     
    270301 
    271302                                if ($type eq 'HASH') { 
    272                                         # Do validation 
    273                                         if ($cmd->{$key}->{VALIDATE}) { 
    274                                         my $type = ref($cmd->{$key}->{VALIDATE}); 
    275  
    276                                                 if (not $type) { 
    277                                                         if ($cmd->{$key}->{VALIDATE} and $cmd->{$key}->{HELP}) { 
    278                                                         my %commands; 
    279  
    280                                                                 $process_help->($cmd->{$key}->{HELP}, \%commands, '<eol>'); 
    281                                                                 return ('ERROR', "Invalid input '$token'.", $ctx) 
    282                                                                         unless $commands{$token}; 
    283                                                         } 
    284                                                 } elsif ($type eq 'CODE') { 
    285                                                         return ('ERROR', "Invalid input '$token'.", $ctx) 
    286                                                                 unless $cmd->{$key}->{VALIDATE}->($ctx, $token, @{$ctx->args()}); 
    287                                                 } elsif ($type eq 'HASH') { 
    288                                                         return ('ERROR', "Invalid input '$token'.", $ctx) 
    289                                                                 unless $cmd->{$key}->{VALIDATE}->{$token}; 
    290                                                 } elsif ($type eq 'ARRAY') { 
    291                                                 my $found = 0; 
    292  
    293                                                         VALIDATE_ARRAY: for my $t (@{$cmd->{$key}->{VALIDATE}}) { 
    294                                                                 $found =1, last VALIDATE_ARRAY 
    295                                                                         if $token eq $t; 
    296                                                         } 
    297                                                         return ('ERROR', "Invalid input '$token'.", $ctx) 
    298                                                                 unless $found; 
    299                                                 } else { 
    300                                                         return ('ERROR', 'Invalid value for VALIDATE target.', $ctx); 
    301                                                 } 
    302                                         } 
    303  
    304303                                        $ctx->inject_token($token) if $rx eq 'ACTION'; 
    305304                                        return $self->parse_node($ctx->next_grammar($cmd->{$key})); 
  • manage/trunk/manage

    r175 r176  
    4343        white => "\e[37m", 
    4444); 
    45 %C = ( bold => "", nounderline => "", underline => "", reset => "", normal => "", black => "",  
    46         red => "", green => "", brown => "", blue => "", magenta => "", cyan => "", 
    47         white => "" ) unless (-t STDOUT); 
    48  
    49 our @ALTCOLOURS = ("$C{cyan}", "$C{cyan}$C{bold}"); 
     45%C = ( bold => "", nobold => "", nounderline => "", underline => "", 
     46        reset => "", normal => "", black => "",  
     47        red => "", green => "", brown => "", blue => "", magenta => "", 
     48        cyan => "", white => "" ) unless (-t STDOUT); 
     49 
     50our @ALTCOLOURS = ("$C{cyan}$C{bold}", "$C{cyan}$C{nobold}"); 
    5051# Global variables 
    5152our $PROMPT = "$SELF> "; 
     
    6162        debug => { 
    6263                value => \$DEBUG, 
    63                 pattern => '\d+', 
     64                pattern => '\d', 
    6465                help => 'Set debug level (0-9).', 
    6566        }, 
     
    225226        my @candidates; 
    226227                push(@th, "") if $action =~ /More input expected/; 
    227         my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th < @ath ? ' ' : ''); 
     228        my $prefix = ' ' x length("@th[0 .. $#th - 1]") . (@th <= @ath ? ' ' : ''); 
    228229 
    229230                $action = "$C{underline}@ath$C{nounderline}\n    $prefix^ $action"; 
     
    259260                        if ($grammar->{ACTION}) { 
    260261                                $action .= "\n    ${prefix}Expected end of command."; 
    261                       } else { 
    262                               $action .= "\n    ${prefix}Can't find any candidates, probably a grammar error." 
     262#                     } else { 
     263#                             $action .= "\n    ${prefix}Can't find any candidates, probably a grammar error." 
    263264                        } 
    264265                } 
     
    313314                                        } 
    314315                                }, 
     316                                VALIDATE => sub { 
     317                                my ($ctx, $value, $pattern) = (shift, shift, $CONF{shift()}->{pattern}); 
     318 
     319                                        $pattern = '.+' unless defined($pattern); 
     320                                        return $value =~ /^$pattern$/; 
     321                                }, 
    315322                                HELP => [ '<value>', 'Set value.' ], 
    316323                        }, 
     324                        VALIDATE => 1, 
    317325                        HELP => sub { 
    318326                        my $ctx = shift;