Module: Parser

Included in:
Naksh::Syntax::Bash, Ren
Defined in:
lib/rust/helpers/parser.rb

Overview

Created by me on 2011-12-23.

Copyright (c) 2011. All pwnage reserved.

Instance Method Summary collapse

Instance Method Details

#run(*arr) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/rust/helpers/parser.rb', line 8

def run(*arr)
  parts, styles = arr[0..-2], arr.last
  res = []
  parts.each_with_index do |part, i|
    cmd = part[0]
    args = part[1]
    res << [cmd, args.join(' ')].join(' ').strip
  end
  run_standard(res, styles)
end

#run_standard(parts, styles) ⇒ Object



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
# File 'lib/rust/helpers/parser.rb', line 19

def run_standard(parts, styles)
  # Please note that this will
  # work for even single commands!
  # :stding is STDIN and :stdout is STDOUT

  # this is the actual pipage
  inp = STDIN
  res = []
  output = nil
  parts.each_with_index do |part, i|
    if builtins.include? part.split(' ').shift.to_sym
      ::CommandCenter.run_as_internal(part)
    else
      # pipe_to returns the pid, in, out, and err
      pipes = ::Pipes.pipe_to(part, :stdin => inp, :style => styles[i])
      inp = pipes[2]
      res.concat pipes[1..-1]
    end
  end
  output = res[-2].read if res[-2] # only pipes get put here, so need to check for that
  # close them all off
  res.each {|pipe| pipe.close unless pipe.closed? }

  output.to_s# + "\n"
end