| 1 | package Psh::Builtins::Alias; | ||
| 2 | |||
| 3 | require Psh::Support::Alias; | ||
| 4 | require Psh::Util; | ||
| 5 | require Psh::Parser; | ||
| 6 | |||
| 7 | =item * C<alias [NAME [=] REPLACEMENT]> | ||
| 8 | |||
| 9 | Add C<I<NAME>> as a built-in so that NAME <REST_OF_LINE> will execute | ||
| 10 | exactly as if REPLACEMENT <REST_OF_LINE> had been entered. For | ||
| 11 | example, one can execute C<alias ls ls -F> to always supply the B<-F> | ||
| 12 | option to "ls". Note the built-in is defined to avoid recursion | ||
| 13 | here. | ||
| 14 | |||
| 15 | With no arguments, prints out a list of the current aliases. | ||
| 16 | With only the C<I<NAME>> argument, prints out a definition of the | ||
| 17 | alias with that name. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub bi_alias | ||
| 22 | { | ||
| 23 | my $line = shift; | ||
| 24 | my @words= @{shift()}; | ||
| 25 | my ($command, $firstDelim, @rest) = Psh::Parser::decompose('([ \t\n=]+)', $line, undef, 0); | ||
| 26 | my $text = join('',@rest); # reconstruct everything after the | ||
| 27 | # first delimiter, sans quotes | ||
| 28 | if (($command eq "") && ($text eq "")) { | ||
| 29 | my $wereThereSome = 0; | ||
| 30 | for $command (sort keys %Psh::Support::Alias::aliases) { | ||
| 31 | my $aliasrhs = $Psh::Support::Alias::aliases{$command}; | ||
| 32 | $aliasrhs =~ s/\'/\\\'/g; | ||
| 33 | Psh::Util::print_out("alias $command='$aliasrhs'\n"); | ||
| 34 | $wereThereSome = 1; | ||
| 35 | } | ||
| 36 | if (!$wereThereSome) { | ||
| 37 | Psh::Util::print_out_i18n('bi_alias_none'); | ||
| 38 | } | ||
| 39 | } elsif( $text eq '') { | ||
| 40 | my $aliasrhs = $Psh::Support::Alias::aliases{$command}; | ||
| 41 | $aliasrhs =~ s/\'/\\\'/g; | ||
| 42 | Psh::Util::print_out("alias $command='$aliasrhs'\n"); | ||
| 43 | } elsif ($text eq '-a') { | ||
| 44 | Psh::Util::print_error_i18n('bi_alias_cant_a'); | ||
| 45 | return (0,undef); | ||
| 46 | } else { | ||
| 47 | $Psh::Support::Alias::aliases{$command} = Psh::Parser::unquote($text); | ||
| 48 | } | ||
| 49 | return (1,undef); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | 1; |