| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # shell::rc Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Shell::Rc; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(custom) ], | ||
| 16 | attributes => { | ||
| 17 | input => [ qw(file) ], | ||
| 18 | create_default => [ qw(0|1) ], | ||
| 19 | }, | ||
| 20 | attributes_default => { | ||
| 21 | create_default => 1, | ||
| 22 | }, | ||
| 23 | commands => { | ||
| 24 | load => [ qw(input|OPTIONAL) ], | ||
| 25 | execute => [ qw($line_list) ], | ||
| 26 | write_default => [ ], | ||
| 27 | load_and_execute => [ qw(input|OPTIONAL) ], | ||
| 28 | }, | ||
| 29 | }; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub brik_use_properties { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | return { | ||
| 36 | attributes_default => { | ||
| 37 | input => defined($self->global) && $self->global->homedir.'/.metabrik_rc' | ||
| 38 | || defined($ENV{HOME}) && $ENV{HOME}.'/.metabrik_rc' || '/tmp/.metabrik_rc', | ||
| 39 | }, | ||
| 40 | }; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub load { | ||
| 44 | my $self = shift; | ||
| 45 | my ($input) = @_; | ||
| 46 | |||
| 47 | $input ||= $self->input; | ||
| 48 | |||
| 49 | if (! -f $input && $self->create_default) { | ||
| 50 | $self->write_default; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (! -f $input && ! $self->create_default) { | ||
| 54 | return $self->log->error("load: can't find rc file [$input]"); | ||
| 55 | } | ||
| 56 | |||
| 57 | my @lines = (); | ||
| 58 | open(my $in, '<', $input) | ||
| 59 | or return $self->log->error("local: can't open rc file [$input]: $!"); | ||
| 60 | while (defined(my $line = <$in>)) { | ||
| 61 | chomp($line); | ||
| 62 | next if $line =~ /^\s*$/; # Skip blank lines | ||
| 63 | next if $line =~ /^\s*#/; # Skip comments | ||
| 64 | $line =~ s/^(.*)#.*$/$1/; # Strip comments at end of line | ||
| 65 | push @lines, "$line "; # Add a trailing slash in case of a multiline | ||
| 66 | # So when joining them, there is no unwanted concatenation | ||
| 67 | } | ||
| 68 | close($in); | ||
| 69 | |||
| 70 | $self->log->debug("load: success"); | ||
| 71 | |||
| 72 | return \@lines; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub execute { | ||
| 76 | my $self = shift; | ||
| 77 | my ($lines) = @_; | ||
| 78 | |||
| 79 | if (! defined($self->shell)) { | ||
| 80 | return $self->log->error("execute: no core::shell Brik"); | ||
| 81 | } | ||
| 82 | |||
| 83 | $self->brik_help_run_undef_arg('execute', $lines) or return; | ||
| 84 | $self->brik_help_run_invalid_arg('execute', $lines, 'ARRAY') or return; | ||
| 85 | |||
| 86 | my $shell = $self->shell; | ||
| 87 | |||
| 88 | $shell->cmdloop($lines); | ||
| 89 | |||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub write_default { | ||
| 94 | my $self = shift; | ||
| 95 | my ($file) = @_; | ||
| 96 | |||
| 97 | $file ||= $self->input; | ||
| 98 | |||
| 99 | if (-f $file) { | ||
| 100 | return $self->log->error("write_default: file [$file] already exists"); | ||
| 101 | } | ||
| 102 | |||
| 103 | open(my $out, '>', $file) | ||
| 104 | or return $self->log->error("write_default: open: file [$file]: $!"); | ||
| 105 | |||
| 106 | my $content = <<EOF; | ||
| 107 | set core::shell echo 0 | ||
| 108 | |||
| 109 | my \$home = \$ENV{HOME} | ||
| 110 | my \$user = \$ENV{USER} | ||
| 111 | |||
| 112 | set core::global ctimeout 5 | ||
| 113 | set core::global rtimeout 5 | ||
| 114 | set core::shell ps1 Meta | ||
| 115 | set core::log level 1 | ||
| 116 | |||
| 117 | use shell::command | ||
| 118 | use shell::history | ||
| 119 | use brik::tool | ||
| 120 | use brik::search | ||
| 121 | |||
| 122 | alias reuse "run core::context reuse" | ||
| 123 | alias pwd "run core::shell pwd" | ||
| 124 | alias update_available "run core::context update_available" | ||
| 125 | alias reuse "run core::context reuse" | ||
| 126 | alias system "run shell::command system" | ||
| 127 | alias capture "run shell::command capture" | ||
| 128 | alias ls "run shell::command capture ls -Fh" | ||
| 129 | alias l "run shell::command capture ls -lFh" | ||
| 130 | alias ll "run shell::command capture ls -lFh" | ||
| 131 | alias cp "run shell::command capture cp -rfp" | ||
| 132 | alias rm "run shell::command capture rm -rf" | ||
| 133 | alias find "run shell::command capture find" | ||
| 134 | alias grep "run shell::command capture grep" | ||
| 135 | alias ! "run shell::history exec" | ||
| 136 | alias history "run shell::history show" | ||
| 137 | alias source "run shell::rc load_and_execute" | ||
| 138 | alias search "run brik::search" | ||
| 139 | alias show "run brik::search all" | ||
| 140 | |||
| 141 | run shell::history load | ||
| 142 | |||
| 143 | set core::shell echo 1 | ||
| 144 | EOF | ||
| 145 | |||
| 146 | print $out $content; | ||
| 147 | |||
| 148 | close($out); | ||
| 149 | |||
| 150 | $self->log->verbose("write_default: default rc file [$file] created"); | ||
| 151 | |||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub load_and_execute { | ||
| 156 | my $self = shift; | ||
| 157 | my ($input) = @_; | ||
| 158 | |||
| 159 | $input ||= $self->input; | ||
| 160 | |||
| 161 | my $lines = $self->load($input) or return; | ||
| 162 | return $self->execute($lines); | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ | ||