Class: Session::Sh

Inherits:
AbstractSession show all
Defined in:
lib/session.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.



449
450
451
# File 'lib/session.rb', line 449

def status
  @status
end

Instance Method Details

#clearObject



453
454
455
456
457
458
459
460
# File 'lib/session.rb', line 453

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



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/session.rb', line 516

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



473
474
475
476
477
478
479
480
# File 'lib/session.rb', line 473

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



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/session.rb', line 485

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



501
502
503
504
# File 'lib/session.rb', line 501

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

#path=(arg) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
# File 'lib/session.rb', line 505

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



461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/session.rb', line 461

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



481
482
483
484
# File 'lib/session.rb', line 481

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