Module: Syscmd

Defined in:
lib/syscmd.rb,
lib/syscmd/popen.rb

Overview

Module.

Defined Under Namespace

Classes: AlreadyExecutedError, Command

Class Method Summary collapse

Class Method Details

.exec!(cmd, *args) ⇒ Object

execute a system command.

cmd

the command to execute

args

command line arguments

returns

executed Syscmd::Command object



104
105
106
107
# File 'lib/syscmd.rb', line 104

def exec!(cmd, *args)
  cmd = Command.new(cmd, args)
  cmd.exec!
end

.popen(*cmd) ⇒ Object

Taken from the original Ruby 1.8 implementation of Open3::open3 and slightly tweaked to return an exit status. This function is meant as in internal method of the Syscmd module; do not use it on it’s own, as the interface may change anytime.

cmd

the command to execute

returns

status, stdout, stderr

orignal author

Yukihiro Matsumoto

orignal socumentation

Konrad Meyer



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
# File 'lib/syscmd/popen.rb', line 12

def popen(*cmd)
  pw = IO::pipe   # pipe[0] for read, pipe[1] for write
  pr = IO::pipe
  pe = IO::pipe
  status = 0 # status of the inner fork

  pid = fork do
    # child
    gcpid = fork do
    	# grandchild
    	pw[1].close
    	STDIN.reopen(pw[0])
    	pw[0].close

    	pr[0].close
    	STDOUT.reopen(pr[1])
    	pr[1].close

    	pe[0].close
    	STDERR.reopen(pe[1])
    	pe[1].close

    	exec(*cmd)
    end
    gcpid, gcstatus = Process.wait2(gcpid)
    exit!(gcstatus.exitstatus)
  end

  pw[0].close
  pr[1].close
  pe[1].close
  pid, status = Process.wait2(pid)
  
  #pi = [status.exitstatus, pw[1], pr[0], pe[0]]
  pi = [status, pr[0], pe[0]]
  pw[1].sync = true
  if defined? yield
    begin
     return yield(*pi)
    ensure
     pi.each{|p| p.close unless p.closed?}
    end
  end
  pi
end