daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest May 16th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module EventMachine
  2.   def self.system3(cmd, *args, &cb)
  3.     cb ||= args.pop if args.last.is_a? Proc
  4.     init = args.pop if args.last.is_a? Proc
  5.  
  6.     # merge remaining arguments into the command
  7.     cmd = ([cmd] + args.map{|a|a.to_s.dump}).join(' ')
  8.  
  9.     new_stderr = $stderr.dup
  10.  
  11.     rd, wr = IO::pipe
  12.  
  13.     result_count = 0
  14.  
  15.     err_result = nil
  16.     std_result = nil
  17.     stderr_connection = nil
  18.  
  19.     err_proc = proc {|output, status|
  20.       stderr_connection = nil
  21.       err_result = output
  22.       result_count+=1
  23.       if result_count == 2
  24.         cb[std_result, err_result, status]
  25.       end
  26.     }
  27.  
  28.     std_proc = proc {|output, status|
  29.       stderr_connection.close_connection if stderr_connection
  30.       rd.close
  31.       std_result = output
  32.       result_count += 1
  33.       if result_count == 2
  34.         cb[std_result, err_result, status]
  35.       end
  36.     }
  37.  
  38.     $stderr.reopen(wr)
  39.     signature = EM.popen(cmd, SystemCmd, std_proc) do |c|
  40.       init[c] if init
  41.     end.signature
  42.     stderr_connection = EM.attach(rd, SystemCmd, err_proc)
  43.     $stderr.reopen(new_stderr)
  44.     wr.close
  45.  
  46.     return EventMachine.get_subprocess_pid(signature)
  47.   end
  48. end
RAW Paste Data
Top