Class: Session::Sh

Inherits:
AbstractSession show all
Defined in:
lib/session.rb,
lib/session-2.4.0.rb

Overview

class AbstractSession

Direct Known Subclasses

Bash

Constant Summary collapse

DEFAULT_PROG =

–{{{

'sh'
ECHO =
'echo'

Instance Attribute Summary collapse

Attributes inherited from AbstractSession

#debug, #errproc, #history, #opts, #outproc, #prog, #stderr, #stdin, #stdout, #threads, #track_history, #use_open3, #use_spawn

Instance Method Summary collapse

Methods inherited from AbstractSession

#__fork, #__popen3, #close!, default_prog, default_prog=, #getopt, #hasopt, init, #initialize, #ready?

Constructor Details

This class inherits a constructor from Session::AbstractSession

Instance Attribute Details

#statusObject (readonly) Also known as: exit_status, exitstatus

Returns the value of attribute status.



497
498
499
# File 'lib/session.rb', line 497

def status
  @status
end

Instance Method Details

#clearObject



501
502
503
504
505
506
507
508
509
510
# File 'lib/session.rb', line 501

def clear
#--{{{
  stdin.puts "#{ ECHO } __clear__ 1>&2"
  stdin.puts "#{ ECHO } __clear__"
  stdin.flush
  while((line = stderr.gets) and line !~ %r/__clear__/o); end
  while((line = stdout.gets) and line !~ %r/__clear__/o); end
  self
#--}}}
end

#execute(command, redirects = {}, &block) ⇒ Object

–}}}



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/session.rb', line 578

def execute(command, redirects = {}, &block)
#--{{{
# setup redirect on stdin
  rin = redirects[:i] || redirects[:in] || redirects[:stdin] || 
         redirects['stdin'] || redirects['i'] || redirects['in'] ||
         redirects[0] || redirects['0']

  if rin
    tmp = 
      begin
        Tempfile::new rand.to_s
      rescue
        Tempfile::new rand.to_s
      end

    begin
      tmp.write(
        if rin.respond_to? 'read'
          rin.read
        elsif rin.respond_to? 'to_s'
          rin.to_s
        else
          rin
        end
      )
      tmp.flush
      command = "{ #{ command } ;} < #{ tmp.path }"
      #puts command
      super(command, redirects, &block)
    ensure
      tmp.close! if tmp 
    end

  else
    super
  end
#--}}}
end

#get_statusObject

–}}}



525
526
527
528
529
530
531
532
533
534
# File 'lib/session.rb', line 525

def get_status
#--{{{
  @status = get_var '__exit_status__' 
  unless @status =~ /^\s*\d+\s*$/o
    raise ExecutionError, "could not determine exit status from <#{ @status.inspect }>"
  end

  @status = Integer @status
#--}}}
end

#get_var(name) ⇒ Object

–}}}



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/session.rb', line 541

def get_var name
#--{{{
  stdin.puts "#{ ECHO } \"#{ name }=${#{ name }}\""
  stdin.flush

  var = nil
  while((line = stdout.gets))
    m = %r/#{ name }\s*=\s*(.*)/.match line
    if m
      var = m[1] 
      raise ExecutionError, "could not determine <#{ name }> from <#{ line.inspect }>" unless var
      break
    end
  end

  var
#--}}}
end

#pathObject

–}}}



559
560
561
562
563
564
# File 'lib/session.rb', line 559

def path 
#--{{{
  var = get_var 'PATH'
  var.strip.split %r/:/o
#--}}}
end

#path=(arg) ⇒ Object

–}}}



565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/session.rb', line 565

def path= arg 
#--{{{
  case arg
    when Array
      arg = arg.join ':'
    else
      arg = arg.to_s.strip
  end

  set_var 'PATH', "'#{ arg }'"
  self.path
#--}}}
end

#send_command(cmd) ⇒ Object

–}}}



511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/session.rb', line 511

def send_command cmd
#--{{{
  stdin.printf "%s '%s' 1>&2\n", ECHO, cmd.begin_err
  stdin.printf "%s '%s' \n", ECHO, cmd.begin_out
 
  stdin.printf "%s\n", cmd.cmd
  stdin.printf "export __exit_status__=$?\n"

  stdin.printf "%s '%s' 1>&2\n", ECHO, cmd.end_err
  stdin.printf "%s '%s' \n", ECHO, cmd.end_out
 
  stdin.flush
#--}}}
end

#set_var(name, value) ⇒ Object

–}}}



535
536
537
538
539
540
# File 'lib/session.rb', line 535

def set_var name, value
#--{{{
  stdin.puts "export #{ name }=#{ value }"
  stdin.flush
#--}}}
end