Class: Shell

Inherits:
Object
  • Object
show all
Extended by:
Exception2MessageMapper
Includes:
Error
Defined in:
lib/shell/filter.rb,
lib/shell.rb,
lib/shell/error.rb,
lib/shell/version.rb,
lib/shell/system-command.rb,
lib/shell/builtin-command.rb,
lib/shell/command-processor.rb,
lib/shell/process-controller.rb

Overview

version.rb - shell version definition file

  $Release Version: 0.6.0$
  $Revision$
  $Date$
  by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)

Defined Under Namespace

Modules: Error Classes: AppendFile, AppendIO, BuiltInCommand, Cat, CommandProcessor, Concat, Echo, Filter, Glob, ProcessController, SystemCommand, Tee

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShell

Returns a new instance of Shell.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/shell.rb', line 79

def initialize
  @cwd = Dir.pwd
  @dir_stack = []
  @umask = nil

  @system_path = Shell.default_system_path
  @record_separator = Shell.default_record_separator

  @command_processor = CommandProcessor.new(self)
  @process_controller = ProcessController.new(self)

  @verbose = Shell.verbose
  @debug = Shell.debug
end

Class Attribute Details

.cascadeObject

Returns the value of attribute cascade.



34
35
36
# File 'lib/shell.rb', line 34

def cascade
  @cascade
end

.debugObject Also known as: debug?

Returns the value of attribute debug.



35
36
37
# File 'lib/shell.rb', line 35

def debug
  @debug
end

.verboseObject Also known as: verbose?

Returns the value of attribute verbose.



36
37
38
# File 'lib/shell.rb', line 36

def verbose
  @verbose
end

Instance Attribute Details

#command_processorObject (readonly)

Returns the value of attribute command_processor.



115
116
117
# File 'lib/shell.rb', line 115

def command_processor
  @command_processor
end

#cwdObject (readonly) Also known as: dir, getwd, pwd

Dir related methods

Shell#cwd/dir/getwd/pwd Shell#chdir/cd Shell#pushdir/pushd Shell#popdir/popd Shell#mkdir Shell#rmdir



134
135
136
# File 'lib/shell.rb', line 134

def cwd
  @cwd
end

#debugObject Also known as: debug?

Returns the value of attribute debug.



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

def debug
  @debug
end

#dir_stackObject (readonly) Also known as: dirs

Returns the value of attribute dir_stack.



139
140
141
# File 'lib/shell.rb', line 139

def dir_stack
  @dir_stack
end

#process_controllerObject (readonly)

Returns the value of attribute process_controller.



116
117
118
# File 'lib/shell.rb', line 116

def process_controller
  @process_controller
end

#record_separatorObject

Returns the value of attribute record_separator.



102
103
104
# File 'lib/shell.rb', line 102

def record_separator
  @record_separator
end

#system_pathObject

Returns the value of attribute system_path.



94
95
96
# File 'lib/shell.rb', line 94

def system_path
  @system_path
end

#umaskObject

Returns the value of attribute umask.



101
102
103
# File 'lib/shell.rb', line 101

def umask
  @umask
end

#verboseObject Also known as: verbose?

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Class Method Details

.alias_command(ali, command, *opts, &block) ⇒ Object



223
224
225
# File 'lib/shell.rb', line 223

def Shell.alias_command(ali, command, *opts, &block)
  CommandProcessor.alias_command(ali, command, *opts, &block)
end

.cd(path) ⇒ Object



48
49
50
51
52
# File 'lib/shell.rb', line 48

def cd(path)
  sh = new
  sh.cd path
  sh
end

.def_system_command(command, path = command) ⇒ Object

command definitions



215
216
217
# File 'lib/shell.rb', line 215

def Shell.def_system_command(command, path = command)
  CommandProcessor.def_system_command(command, path)
end

.default_record_separatorObject



66
67
68
69
70
71
72
# File 'lib/shell.rb', line 66

def default_record_separator
  if @default_record_separator
  @default_record_separator
  else
  $/
  end
end

.default_record_separator=(rs) ⇒ Object



74
75
76
# File 'lib/shell.rb', line 74

def default_record_separator=(rs)
  @default_record_separator = rs
end

.default_system_pathObject



54
55
56
57
58
59
60
# File 'lib/shell.rb', line 54

def default_system_path
  if @default_system_path
  @default_system_path
  else
  ENV["PATH"].split(":")
  end
end

.default_system_path=(path) ⇒ Object



62
63
64
# File 'lib/shell.rb', line 62

def default_system_path=(path)
  @default_system_path = path
end

.install_system_commands(pre = "sys_") ⇒ Object



231
232
233
# File 'lib/shell.rb', line 231

def Shell.install_system_commands(pre = "sys_")
  CommandProcessor.install_system_commands(pre)
end

.notify(*opts, &block) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/shell.rb', line 244

def self.notify(*opts, &block)
  Thread.exclusive do
  if opts[-1].kind_of?(String)
    yorn = verbose?
  else
    yorn = opts.pop
  end
  return unless yorn

  _head = true
  print opts.collect{|mes|
    mes = mes.dup
    yield mes if iterator?
    if _head
  _head = false
  "shell: " + mes
    else
  "       " + mes
    end
  }.join("\n")+"\n"
  end
end

.unalias_command(ali) ⇒ Object



227
228
229
# File 'lib/shell.rb', line 227

def Shell.unalias_command(ali)
  CommandProcessor.unalias_command(ali)
end

.undef_system_command(command) ⇒ Object



219
220
221
# File 'lib/shell.rb', line 219

def Shell.undef_system_command(command)
  CommandProcessor.undef_system_command(command)
end

Instance Method Details

#chdir(path = nil) ⇒ Object Also known as: cd

If called as iterator, it restores the current directory when the block ends.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/shell.rb', line 144

def chdir(path = nil)
  if iterator?
    cwd_old = @cwd
    begin
  chdir(path)
  yield
    ensure
  chdir(cwd_old)
    end
  else
    path = "~" unless path
    @cwd = expand_path(path)
    notify "current dir: #{@cwd}"
    rehash
    self
  end
end

#expand_path(path) ⇒ Object



118
119
120
# File 'lib/shell.rb', line 118

def expand_path(path)
  File.expand_path(path, @cwd)
end

#inspectObject



236
237
238
239
240
241
242
# File 'lib/shell.rb', line 236

def inspect
  if debug.kind_of?(Integer) && debug > 2
    super
  else
    to_s
  end
end

#jobsObject

process management



204
205
206
# File 'lib/shell.rb', line 204

def jobs
  @process_controller.jobs
end

#kill(sig, command) ⇒ Object



208
209
210
# File 'lib/shell.rb', line 208

def kill(sig, command)
  @process_controller.kill_job(sig, command)
end

#popdirObject Also known as: popd



189
190
191
192
193
194
195
196
197
# File 'lib/shell.rb', line 189

def popdir
  if pop = @dir_stack.pop
    chdir pop
    notify "dir stack: [#{@dir_stack.join ', '}]"
    self
  else
    Shell.Fail DirStackEmpty
  end
end

#pushdir(path = nil) ⇒ Object Also known as: pushd



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/shell.rb', line 163

def pushdir(path = nil)
  if iterator?
    pushdir(path)
    begin
  yield
    ensure
  popdir
    end
  elsif path
    @dir_stack.push @cwd
    chdir path
    notify "dir stack: [#{@dir_stack.join ', '}]"
    self
  else
    if pop = @dir_stack.pop
  @dir_stack.push @cwd
  chdir pop
  notify "dir stack: [#{@dir_stack.join ', '}]"
  self
    else
  Shell.Fail DirStackEmpty
    end
  end
end