lib/shell/builtin-command.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   shell/builtin-command.rb - 
   3  #       $Release Version: 0.6.0 $
   4  #       $Revision: 1.1 $
   5  #       $Date: 2001/05/17 10:02:48 $
   6  #       by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  12  
  13  require "shell/filter"
  14  
  15  class Shell
  16    class BuiltInCommand<Filter
  17      def wait?
  18        false
  19      end
  20      def active?
  21        true
  22      end
  23    end
  24  
  25    class Echo < BuiltInCommand
  26      def initialize(sh, *strings)
  27        super sh
  28        @strings = strings
  29      end
  30      
  31      def each(rs = nil)
  32        rs =  @shell.record_separator unless rs
  33        for str  in @strings
  34          yield str + rs
  35        end
  36      end
  37    end
  38  
  39    class Cat < BuiltInCommand
  40      def initialize(sh, *filenames)
  41        super sh
  42        @cat_files = filenames
  43      end
  44  
  45      def each(rs = nil)
  46        if @cat_files.empty?
  47          super
  48        else
  49          for src in @cat_files
  50            @shell.foreach(src, rs){|l| yield l}
  51          end
  52        end
  53      end
  54    end
  55  
  56    class Glob < BuiltInCommand
  57      def initialize(sh, pattern)
  58        super sh
  59  
  60        @pattern = pattern
  61        Thread.critical = true
  62        back = Dir.pwd
  63        begin
  64          Dir.chdir @shell.cwd
  65          @files = Dir[pattern]
  66        ensure
  67          Dir.chdir back
  68          Thread.critical = false
  69        end
  70      end
  71  
  72      def each(rs = nil)
  73        rs =  @shell.record_separator unless rs
  74        for f  in @files
  75          yield f+rs
  76        end
  77      end
  78    end
  79  
  80  #   class Sort < Cat
  81  #     def initialize(sh, *filenames)
  82  #       super
  83  #     end
  84  #
  85  #     def each(rs = nil)
  86  #       ary = []
  87  #       super{|l|       ary.push l}
  88  #       for l in ary.sort!
  89  #       yield l
  90  #       end
  91  #     end
  92  #   end
  93  
  94    class AppendIO < BuiltInCommand
  95      def initialize(sh, io, filter)
  96        super sh
  97        @input = filter
  98        @io = io
  99      end
 100  
 101      def input=(filter)
 102        @input.input=filter
 103        for l in @input
 104          @io << l
 105        end
 106      end
 107  
 108    end
 109  
 110    class AppendFile < AppendIO
 111      def initialize(sh, to_filename, filter)
 112        @file_name = to_filename
 113        io = sh.open(to_filename, "a")
 114        super(sh, io, filter)
 115      end
 116  
 117      def input=(filter)
 118        begin
 119          super
 120        ensure
 121          @io.close
 122        end
 123      end
 124    end
 125  
 126    class Tee < BuiltInCommand
 127      def initialize(sh, filename)
 128        super sh
 129        @to_filename = filename
 130      end
 131  
 132      def each(rs = nil)
 133        to = @shell.open(@to_filename, "w")
 134        begin
 135          super{|l| to << l; yield l}
 136        ensure
 137          to.close
 138        end
 139      end
 140    end
 141  
 142    class Concat < BuiltInCommand
 143      def initialize(sh, *jobs)
 144        super(sh)
 145        @jobs = jobs
 146      end
 147  
 148      def each(rs = nil)
 149        while job = @jobs.shift
 150          job.each{|l| yield l}
 151        end
 152      end
 153    end
 154  end