Skip to content
Kward Search API index

Class: Kward::NativeShell::Runtime

Inherits:
Object
  • Object
show all
Includes:
Arrays, Builtins, Condition, Expansion, HistoryCommands, Interactive, Jobs, Locals, ProgrammableCompletion, Substitution, Traps
Defined in:
lib/kward/shell/native_runtime.rb

Overview

Owns native shell state. External commands are always spawned with argv, never a command string, and executable text has no implicit sh fallback.

Constant Summary collapse

UNSUPPORTED =
%w[typeset builtin].freeze
SPECIAL =
%w[export readonly eval . source unset exit return break continue shift set exec trap times :].freeze
BUILTINS =
(SPECIAL + %w[true false cd pwd alias unalias read getopts command shopt local declare complete compgen umask ulimit kwsh jobs fg bg wait kill hash type which fc]).freeze

Constants included from Traps

Traps::INTERACTIVE_SIGNALS, Traps::RUBY_RESERVED_SIGNALS

Constants included from Jobs

Jobs::MAX_JOBS, Jobs::TCSETPGRP

Constants included from ProgrammableCompletion

ProgrammableCompletion::COMPLETION_BYTES, ProgrammableCompletion::COMPLETION_COUNT, ProgrammableCompletion::COMPLETION_TIMEOUT

Constants included from Condition

Condition::BINARY, Condition::UNARY

Constants included from Substitution

Substitution::MAX_CAPTURE_BYTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traps

#finish, #poll_signals, #with_prompt_input, #with_signals

Methods included from Jobs

#close, #notify_jobs

Methods included from Interactive

#check_mail, #complete_line, #option_enabled?, #prompt_text, #run_shell_agent_command, #vi_edit, #vi_expansions, #vi_input_error

Methods included from HistoryCommands

#history, #with_history_entry

Constructor Details

#initialize(cwd: Dir.pwd, env: ENV.to_h, aliases: {}, arguments: [], name: "kwsh", interactive: false, replace_process: false, descriptors: {}, own_descriptors: false, hosted: interactive, directory: nil, process_owned: false) ⇒ Runtime

Returns a new instance of Runtime.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kward/shell/native_runtime.rb', line 57

def initialize(cwd: Dir.pwd, env: ENV.to_h, aliases: {}, arguments: [], name: "kwsh", interactive: false, replace_process: false, descriptors: {}, own_descriptors: false, hosted: interactive, directory: nil, process_owned: false)
  @directory = directory ? directory.dup : Directory.new(cwd)
  @cwd = File.expand_path(cwd)
  @cwd = initial_directory(env["PWD"]) unless hosted
  @variables = env.dup
  @variables["PWD"] = @cwd
  @exported = Set.new(@variables.keys)
  @variables["PPID"] = Process.ppid.to_s
  @variables["OPTIND"] = "1"
  @variables["IFS"] = Fields::DEFAULT_IFS
  @variables["LINENO"] = "1"
  @line_tracking = true
  @aliases = aliases.dup
  @arguments = arguments
  @name = name
  @shell_pid = Process.pid
  @process_owned = process_owned
  @functions = {}
  @local_scopes = []
  @completion_functions = {}
  @command_hash = {}
  @arrays = {}
  @readonly = Set.new
  @source_depth = 0
  @eval_depth = 0
  @globstar = false
  @options = Set.new(interactive ? ["monitor"] : [])
  @jobs = {}
  @entry_ignored = Set.new(NativeProcess.ignored_signals.map { |name| Signal.list.key(Signal.list.fetch(name)) })
  # Keep the shell's own job-control ignores from becoming job defaults,
  # even when the launcher already ignored these signals.
  @entry_ignored.subtract(%w[TSTP TTIN TTOU]) if interactive || hosted
  @traps = @entry_ignored.to_h { |name| [name, ""] }
  @pending_signals = []
  @persistent_descriptors = descriptors.dup
  @owned_descriptors = own_descriptors ? descriptors.values.compact.uniq : []
  @active_streams = []
  @replace_process = replace_process
  @interactive = interactive
  @hosted = hosted
  @umask = File.umask
  @status = 0
  @exited = false
  @loop_depth = 0
  @function_depth = 0
end

Instance Attribute Details

#aliases ⇒ Object (readonly)

Returns the value of attribute aliases.



55
56
57
# File 'lib/kward/shell/native_runtime.rb', line 55

def aliases
  @aliases
end

#exited ⇒ Object (readonly)

Returns the value of attribute exited.



55
56
57
# File 'lib/kward/shell/native_runtime.rb', line 55

def exited
  @exited
end

#status ⇒ Object (readonly)

Returns the value of attribute status.



55
56
57
# File 'lib/kward/shell/native_runtime.rb', line 55

def status
  @status
end

#variables ⇒ Object (readonly)

Returns the value of attribute variables.



55
56
57
# File 'lib/kward/shell/native_runtime.rb', line 55

def variables
  @variables
end

Instance Method Details

#cwd ⇒ Object



104
105
106
107
108
# File 'lib/kward/shell/native_runtime.rb', line 104

def cwd
  @directory.same?(@cwd) ? @cwd : @directory.physical_path
rescue SystemCallError
  @cwd
end

#environment ⇒ Object



110
111
112
# File 'lib/kward/shell/native_runtime.rb', line 110

def environment
  @variables.select { |key, _| @exported.include?(key) && !@arrays.key?(key) }
end

#expand_parameters(value) ⇒ Object



143
144
145
146
147
# File 'lib/kward/shell/native_runtime.rb', line 143

def expand_parameters(value)
  word = Lexer.new(value).heredoc_word
  validate_parameter_text(word)
  scalar(word)
end

#interrupt_input ⇒ Object

ENV and interactive prompts require parameter expansion, not evaluation of arbitrary command text or command substitutions from the environment.



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

def interrupt_input
  @status = 130
end

#run(source, input: $stdin, output: $stdout, error: $stderr) ⇒ Object



114
115
116
# File 'lib/kward/shell/native_runtime.rb', line 114

def run(source, input: $stdin, output: $stdout, error: $stderr)
  run_input(StringIO.new(source), input: input, output: output, error: error)
end

#run_input(source, input: $stdin, output: $stdout, error: $stderr) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/kward/shell/native_runtime.rb', line 118

def run_input(source, input: $stdin, output: $stdout, error: $stderr)
  @base_streams = { 0 => input, 1 => output, 2 => error }.merge(@persistent_descriptors)
  with_signal_handlers { evaluate_program(source, @base_streams) }
rescue Flow => flow
  if %i[exit abort].include?(flow.kind)
    @exited = true if flow.kind == :exit
    @status = flow.status
  else
    error&.puts("kwsh: #{flow.kind}: not in a valid context")
    @status = 2
  end
rescue SyntaxError, ExpansionError, ExecutionError, SystemCallError, IOError, ArgumentError => e
  error&.puts("kwsh: #{e.message}")
  @exited = true unless interactive_scope?
  @status = 2
rescue Interrupt
  @status = 130
end

#with_pager_suppressed ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/kward/shell/native_runtime.rb', line 149

def with_pager_suppressed
  previous = @variables["GIT_PAGER"]
  exported = @exported.include?("GIT_PAGER")
  @variables["GIT_PAGER"] = "cat"
  @exported.add("GIT_PAGER")
  yield
ensure
  previous ? @variables["GIT_PAGER"] = previous : @variables.delete("GIT_PAGER")
  @exported.delete("GIT_PAGER") unless exported
end