File: Csh.pm

package info (click to toggle)
rex 1.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,012 kB
  • sloc: perl: 35,587; xml: 264; sh: 51; makefile: 13
file content (128 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

package Rex::Interface::Shell::Csh;

use v5.12.5;
use warnings;

our $VERSION = '1.14.1'; # VERSION

use Rex::Interface::Shell::Base;
use base qw(Rex::Interface::Shell::Base);

sub new {
  my $class = shift;
  my $proto = ref($class) || $class;
  my $self  = $proto->SUPER::new(@_);

  bless( $self, $class );

  return $self;
}

sub path {
  my ( $self, $path ) = @_;
  $self->{path} = $path;
}

sub source_global_profile {
  my ( $self, $parse ) = @_;
  $self->{source_global_profile} = $parse;
}

sub source_profile {
  my ( $self, $parse ) = @_;
  $self->{source_profile} = $parse;
}

sub set_locale {
  my ( $self, $locale ) = @_;
  $self->{locale} = $locale;
}

# sub set_env {
#   my ( $self, $env ) = @_;
#   my $cmd = undef;
#
#   die("Error: env must be a hash")
#     if ( ref $env ne "HASH" );
#
#   while ( my ( $k, $v ) = each($env) ) {
#     $cmd .= "setenv $k \"$v\"; ";
#     $self->{env_raw} .= "$k $v";
#   }
#
#   $self->{env} = $cmd;
# }

sub exec {
  my ( $self, $cmd, $option ) = @_;

  if ( exists $option->{path} ) {
    $self->path( $option->{path} );
  }

  # if ( exists $option->{env} ) {
  #   $self->set_env( $option->{env} );
  # }

  my $complete_cmd = $cmd;

  # if we need to execute without an sh command,
  # use the format_cmd key
  if ( exists $option->{no_sh} ) {
    $complete_cmd = $option->{format_cmd};
  }

  if ( exists $option->{cwd} ) {
    $complete_cmd = "cd $option->{cwd} && $complete_cmd";
  }

  if ( $self->{path} && !exists $self->{__env__}->{PATH} ) {
    $complete_cmd = "set PATH=$self->{path}; $complete_cmd ";
  }

  if ( $self->{locale} ) {
    $complete_cmd = "set LC_ALL=$self->{locale} ; $complete_cmd ";
  }

  if ( $self->{source_profile} ) {

    # csh is using .login
    $complete_cmd = "source ~/.login >& /dev/null ; $complete_cmd";
  }

  if ( $self->{source_global_profile} ) {
    $complete_cmd = "source /etc/profile >& /dev/null ; $complete_cmd";
  }

  if ( exists $self->{__env__} ) {
    if ( ref $self->{__env__} eq "HASH" ) {
      for my $key ( keys %{ $self->{__env__} } ) {
        my $val = $self->{__env__}->{$key};
        $val =~ s/"/"'"'"/gms;
        $complete_cmd = " setenv $key \"$val\" ; $complete_cmd ";
      }
    }
    else {
      $complete_cmd = $self->{__env__} . " $complete_cmd ";
    }
  }

# this is due to a strange behaviour with Net::SSH2 / libssh2
# it may occur when you run rex inside a kvm virtualized host connecting to another virtualized vm on the same hardware
  if ( Rex::Config->get_sleep_hack ) {
    $complete_cmd .= " ; set f=\$? ; sleep .00000001 ; exit \$f";
  }

  # rewrite the command again
  if ( exists $option->{format_cmd} ) {
    $complete_cmd =~ s/\{\{CMD\}\}/$cmd/;
  }

  return $complete_cmd;
}

1;