kward 0.85.0 → 0.86.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +130 -0
- data/CONTRIBUTING.md +1 -1
- data/Gemfile.lock +6 -2
- data/README.md +3 -3
- data/Rakefile +3 -0
- data/doc/agent-tools.md +11 -0
- data/doc/api.md +4 -1
- data/doc/composer.md +4 -3
- data/doc/configuration.md +60 -11
- data/doc/extensibility.md +2 -0
- data/doc/getting-started.md +3 -3
- data/doc/git.md +1 -1
- data/doc/memory.md +1 -1
- data/doc/plugins.md +40 -5
- data/doc/providers.md +10 -4
- data/doc/rpc.md +125 -7
- data/doc/session-management.md +36 -1
- data/doc/shell.md +643 -27
- data/doc/tabs.md +2 -1
- data/doc/troubleshooting.md +1 -1
- data/doc/usage.md +21 -11
- data/examples/plugins/openrouter_proxy/README.md +103 -0
- data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
- data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
- data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
- data/examples/plugins/openrouter_proxy/server.rb +186 -0
- data/examples/plugins/space_invaders.rb +0 -1
- data/exe/kward +2 -2
- data/exe/kwsh +7 -0
- data/kward.gemspec +3 -1
- data/lib/kward/agent.rb +43 -9
- data/lib/kward/cli/commands.rb +41 -11
- data/lib/kward/cli/interactive_turn.rb +10 -0
- data/lib/kward/cli/plugins.rb +5 -1
- data/lib/kward/cli/prompt_interface.rb +4 -1
- data/lib/kward/cli/rendering.rb +21 -28
- data/lib/kward/cli/runtime_helpers.rb +52 -34
- data/lib/kward/cli/settings/model.rb +1 -12
- data/lib/kward/cli/tabs.rb +91 -18
- data/lib/kward/cli/tool_summaries.rb +2 -0
- data/lib/kward/cli/transcript_formatter.rb +3 -10
- data/lib/kward/cli/worktrees.rb +1 -0
- data/lib/kward/cli.rb +56 -7
- data/lib/kward/config/core.rb +16 -5
- data/lib/kward/config/extensions.rb +3 -0
- data/lib/kward/config/paths.rb +11 -0
- data/lib/kward/config/settings.rb +7 -0
- data/lib/kward/conversation.rb +11 -1
- data/lib/kward/entrypoint.rb +14 -0
- data/lib/kward/events.rb +2 -0
- data/lib/kward/generated_images.rb +237 -0
- data/lib/kward/model/catalog.rb +6 -3
- data/lib/kward/model/client.rb +105 -36
- data/lib/kward/model/codex_images.rb +83 -0
- data/lib/kward/model/copilot_models.rb +8 -4
- data/lib/kward/model/model_info.rb +27 -9
- data/lib/kward/model/openrouter_model_cache.rb +4 -2
- data/lib/kward/model/payloads.rb +6 -5
- data/lib/kward/model/sources.rb +3 -1
- data/lib/kward/model/stream_parser.rb +4 -7
- data/lib/kward/pan/index.html.erb +8 -1
- data/lib/kward/pan/server.rb +13 -1
- data/lib/kward/plugins/codex_responses.rb +73 -0
- data/lib/kward/plugins/host.rb +16 -0
- data/lib/kward/plugins/http_server.rb +83 -0
- data/lib/kward/plugins/registry.rb +39 -0
- data/lib/kward/prompt_interface/composer_controller.rb +0 -1
- data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
- data/lib/kward/prompt_interface/editor/controller.rb +1 -1
- data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
- data/lib/kward/prompt_interface/file_overlay.rb +3 -3
- data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
- data/lib/kward/prompt_interface/git_prompt.rb +18 -11
- data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
- data/lib/kward/prompt_interface/interactive/state.rb +18 -20
- data/lib/kward/prompt_interface/key_handler.rb +108 -25
- data/lib/kward/prompt_interface/layout.rb +11 -1
- data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
- data/lib/kward/prompt_interface/project_browser.rb +27 -54
- data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
- data/lib/kward/prompt_interface/question_prompt.rb +19 -28
- data/lib/kward/prompt_interface/runtime_state.rb +31 -9
- data/lib/kward/prompt_interface/screen.rb +24 -21
- data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
- data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
- data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
- data/lib/kward/prompt_interface.rb +109 -39
- data/lib/kward/prompts.rb +7 -1
- data/lib/kward/pty/adaptive_output_sink.rb +38 -4
- data/lib/kward/pty/interactive_runner.rb +20 -28
- data/lib/kward/pty/transcript_normalizer.rb +65 -21
- data/lib/kward/rpc/config_manager.rb +3 -0
- data/lib/kward/rpc/server.rb +27 -5
- data/lib/kward/rpc/session_manager.rb +62 -17
- data/lib/kward/rpc/transcript_normalizer.rb +11 -7
- data/lib/kward/sessions/store.rb +30 -3
- data/lib/kward/shell/kwsh.rb +27 -7
- data/lib/kward/shell/kwshrc.rb +11 -2
- data/lib/kward/shell/native_arithmetic.rb +255 -0
- data/lib/kward/shell/native_arrays.rb +102 -0
- data/lib/kward/shell/native_braces.rb +96 -0
- data/lib/kward/shell/native_builtins.rb +476 -0
- data/lib/kward/shell/native_completion.rb +169 -0
- data/lib/kward/shell/native_condition.rb +75 -0
- data/lib/kward/shell/native_directory.rb +116 -0
- data/lib/kward/shell/native_exec_state.rb +73 -0
- data/lib/kward/shell/native_expansion.rb +252 -0
- data/lib/kward/shell/native_fields.rb +62 -0
- data/lib/kward/shell/native_glob.rb +96 -0
- data/lib/kward/shell/native_history.rb +242 -0
- data/lib/kward/shell/native_interactive.rb +164 -0
- data/lib/kward/shell/native_invocation.rb +42 -0
- data/lib/kward/shell/native_jobs.rb +250 -0
- data/lib/kward/shell/native_line_editor.rb +382 -0
- data/lib/kward/shell/native_line_terminal.rb +286 -0
- data/lib/kward/shell/native_locale.rb +109 -0
- data/lib/kward/shell/native_locals.rb +57 -0
- data/lib/kward/shell/native_pattern.rb +200 -0
- data/lib/kward/shell/native_process.rb +279 -0
- data/lib/kward/shell/native_program.rb +40 -0
- data/lib/kward/shell/native_programmable_completion.rb +115 -0
- data/lib/kward/shell/native_prompt_format.rb +89 -0
- data/lib/kward/shell/native_runtime.rb +964 -0
- data/lib/kward/shell/native_substitution.rb +110 -0
- data/lib/kward/shell/native_syntax.rb +946 -0
- data/lib/kward/shell/native_traps.rb +196 -0
- data/lib/kward/shell/native_vi.rb +407 -0
- data/lib/kward/shell/native_vi_terminal.rb +19 -0
- data/lib/kward/shell/native_worker.rb +82 -0
- data/lib/kward/shell/persistent_session.rb +184 -190
- data/lib/kward/shell/prompt.rb +14 -0
- data/lib/kward/shell/prompt_agent.rb +17 -0
- data/lib/kward/shell/script_runner.rb +244 -0
- data/lib/kward/shell/standalone_agent.rb +31 -0
- data/lib/kward/shell/standalone_link.rb +78 -0
- data/lib/kward/shell/standalone_terminal.rb +287 -0
- data/lib/kward/terminal/keys.rb +22 -1
- data/lib/kward/terminal/locale_text.rb +29 -0
- data/lib/kward/terminal/sequences.rb +45 -4
- data/lib/kward/tools/datetime.rb +85 -0
- data/lib/kward/tools/generate_image.rb +36 -0
- data/lib/kward/tools/registry.rb +35 -9
- data/lib/kward/tools/tool_call.rb +3 -1
- data/lib/kward/transcripts/markdown_transcript.rb +8 -0
- data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
- data/lib/kward/transport/gateway.rb +2 -1
- data/lib/kward/turn_summary.rb +68 -0
- data/lib/kward/version.rb +1 -1
- data/lib/main.rb +6 -3
- metadata +84 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "tmpdir"
|
|
3
|
+
require "tempfile"
|
|
4
|
+
|
|
5
|
+
module Kward
|
|
6
|
+
module NativeShell
|
|
7
|
+
# Standalone command history. No implicit disk storage; unsafe/unavailable
|
|
8
|
+
# HISTFILE paths fall back to memory without altering their permissions.
|
|
9
|
+
class History
|
|
10
|
+
attr_reader :entries, :next_number
|
|
11
|
+
|
|
12
|
+
def initialize(variables, directory:)
|
|
13
|
+
size = variables["HISTSIZE"]
|
|
14
|
+
@size = size&.match?(/\A[0-9]+\z/) ? size.to_i : 512
|
|
15
|
+
@entries = []
|
|
16
|
+
@next_number = 1
|
|
17
|
+
@owner = Process.pid
|
|
18
|
+
path = variables["HISTFILE"]
|
|
19
|
+
@path = directory.resolve(path) if path && !path.empty?
|
|
20
|
+
refresh
|
|
21
|
+
rescue SystemCallError, IOError, ArgumentError
|
|
22
|
+
@path = nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def paused?
|
|
26
|
+
@paused == true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def paused=(paused)
|
|
30
|
+
discard_pending if paused
|
|
31
|
+
@paused = paused
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def with_entry(text)
|
|
35
|
+
return yield if paused?
|
|
36
|
+
|
|
37
|
+
@pending = text unless text.strip.empty?
|
|
38
|
+
yield
|
|
39
|
+
ensure
|
|
40
|
+
commit_pending
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def commit_pending
|
|
44
|
+
pending, @pending = @pending, nil
|
|
45
|
+
add(pending) if pending
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def discard_pending
|
|
49
|
+
@pending = nil
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def add(text)
|
|
53
|
+
return if paused? || text.strip.empty?
|
|
54
|
+
text = text.chomp
|
|
55
|
+
saved = with_lock do
|
|
56
|
+
entries = read_entries
|
|
57
|
+
number = [@next_number, entries.last&.first.to_i + 1].max
|
|
58
|
+
entries = (entries << [number, text]).last(@size)
|
|
59
|
+
persist(entries)
|
|
60
|
+
update_entries(entries, number + 1)
|
|
61
|
+
end
|
|
62
|
+
update_entries((@entries + [[@next_number, text]]).last(@size), @next_number + 1) unless saved
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def refresh
|
|
66
|
+
with_lock do
|
|
67
|
+
entries = read_entries
|
|
68
|
+
update_entries(entries.last(@size), entries.last&.first.to_i + 1)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def select(first, last, listing:)
|
|
73
|
+
raise ExecutionError, "fc: history is empty" if @entries.empty?
|
|
74
|
+
first ||= listing ? "-16" : "-1"
|
|
75
|
+
last ||= listing ? "-1" : first
|
|
76
|
+
from = index(first, range: listing || first != last)
|
|
77
|
+
to = index(last, range: listing || first != last)
|
|
78
|
+
selected = @entries[[from, to].min..[from, to].max]
|
|
79
|
+
from > to ? selected.reverse : selected
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def index(value, range:)
|
|
85
|
+
index = if value.match?(/\A-[0-9]+\z/)
|
|
86
|
+
@entries.length + value.to_i
|
|
87
|
+
elsif value.match?(/\A\+?[0-9]+\z/)
|
|
88
|
+
@entries.index { |number, _| number == value.to_i } || (value.to_i < @entries.first[0] ? -1 : @entries.length)
|
|
89
|
+
else
|
|
90
|
+
@entries.rindex { |_, text| text.start_with?(value) }
|
|
91
|
+
end
|
|
92
|
+
raise ExecutionError, "fc: history entry not found: #{value}" unless index && (range || index.between?(0, @entries.length - 1))
|
|
93
|
+
index.clamp(0, @entries.length - 1)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def with_lock
|
|
97
|
+
return unless @path && Process.pid == @owner
|
|
98
|
+
|
|
99
|
+
flags = File::RDWR | File::CREAT | File::NOFOLLOW | File::NONBLOCK
|
|
100
|
+
File.open("#{@path}.lock", flags, 0600) do |lock|
|
|
101
|
+
validate_file(lock)
|
|
102
|
+
lock.flock(File::LOCK_EX)
|
|
103
|
+
yield
|
|
104
|
+
end
|
|
105
|
+
rescue SystemCallError, IOError, ArgumentError, JSON::ParserError
|
|
106
|
+
@path = nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def validate_file(file)
|
|
110
|
+
stat = file.stat
|
|
111
|
+
return if stat.file? && stat.uid == Process.uid && stat.nlink == 1 && (stat.mode & 0777) == 0600
|
|
112
|
+
|
|
113
|
+
raise IOError, "unsafe history file"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def read_entries
|
|
117
|
+
entries = []
|
|
118
|
+
File.open(@path, File::RDONLY | File::NOFOLLOW | File::NONBLOCK) do |file|
|
|
119
|
+
validate_file(file)
|
|
120
|
+
file.each_line do |line|
|
|
121
|
+
entry = JSON.parse(line)
|
|
122
|
+
if entry.is_a?(Array) && entry[1].is_a?(Hash) && entry[1].keys == ["bytes"] && entry[1]["bytes"].is_a?(String)
|
|
123
|
+
entry[1] = entry[1]["bytes"].unpack1("m0")
|
|
124
|
+
end
|
|
125
|
+
unless entry.is_a?(Array) && entry.size == 2 && entry[0].is_a?(Integer) && entry[0] > entries.last&.first.to_i && entry[1].is_a?(String)
|
|
126
|
+
raise ArgumentError, "invalid history"
|
|
127
|
+
end
|
|
128
|
+
entries << entry
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
entries
|
|
132
|
+
rescue Errno::ENOENT
|
|
133
|
+
[]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def persist(entries)
|
|
137
|
+
limit = Process.getrlimit(:FSIZE).first
|
|
138
|
+
bytes = 0
|
|
139
|
+
Tempfile.create(".kwsh-history-", File.dirname(@path)) do |file|
|
|
140
|
+
file.binmode
|
|
141
|
+
entries.each do |number, text|
|
|
142
|
+
value = text.dup.force_encoding(Encoding::UTF_8)
|
|
143
|
+
value = { "bytes" => [text].pack("m0") } unless value.valid_encoding?
|
|
144
|
+
record = JSON.generate([number, value]) + "\n"
|
|
145
|
+
bytes += record.bytesize
|
|
146
|
+
# The kernel can send SIGXFSZ before a write raises EFBIG.
|
|
147
|
+
raise IOError, "history exceeds the file size limit" if limit != Process::RLIM_INFINITY && bytes > limit
|
|
148
|
+
file.write(record)
|
|
149
|
+
end
|
|
150
|
+
file.fsync
|
|
151
|
+
File.rename(file.path, @path)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def update_entries(entries, next_number)
|
|
156
|
+
@entries = entries
|
|
157
|
+
@next_number = [@next_number, next_number].max
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
module HistoryCommands
|
|
163
|
+
def history
|
|
164
|
+
raise ExecutionError, "fc: native history is standalone-only; hosted input owns its history" if @hosted
|
|
165
|
+
@history ||= History.new(@variables, directory: @directory)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def with_history_entry(text)
|
|
169
|
+
return yield if @hosted
|
|
170
|
+
history.with_entry(text) { yield }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
private
|
|
174
|
+
|
|
175
|
+
def fc_builtin(args, streams)
|
|
176
|
+
flags = []
|
|
177
|
+
editor = nil
|
|
178
|
+
while args.first&.start_with?("-") && !args.first.match?(/\A-[0-9]+\z/)
|
|
179
|
+
option = args.shift
|
|
180
|
+
break if option == "--"
|
|
181
|
+
chars = option.delete_prefix("-").chars
|
|
182
|
+
until chars.empty?
|
|
183
|
+
flag = chars.shift
|
|
184
|
+
raise ExecutionError, "fc: unsupported option -#{flag}" unless "elnrs".include?(flag)
|
|
185
|
+
flags << flag
|
|
186
|
+
if flag == "e"
|
|
187
|
+
editor = chars.empty? ? args.shift : chars.join
|
|
188
|
+
raise ExecutionError, "fc: -e requires an editor" unless editor
|
|
189
|
+
chars.clear
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
listing = flags.include?("l")
|
|
194
|
+
direct = flags.include?("s")
|
|
195
|
+
raise ExecutionError, "fc: incompatible options" if direct && (flags - ["s"]).any? || listing && editor || flags.include?("n") && !listing
|
|
196
|
+
substitution = args.shift.split("=", 2) if direct && args.first&.include?("=")
|
|
197
|
+
raise ExecutionError, "fc: too many operands" if args.length > (direct ? 1 : 2)
|
|
198
|
+
selected = history.select(args[0], args[1], listing: listing)
|
|
199
|
+
selected.reverse! if flags.include?("r")
|
|
200
|
+
if listing
|
|
201
|
+
selected.each do |number, text|
|
|
202
|
+
lines = text.lines(chomp: true)
|
|
203
|
+
write_line(streams, "#{flags.include?('n') ? '' : number}\t#{lines.shift}")
|
|
204
|
+
lines.each { |line| write_line(streams, "\t#{line}") }
|
|
205
|
+
end
|
|
206
|
+
return 0
|
|
207
|
+
end
|
|
208
|
+
history.discard_pending
|
|
209
|
+
source = selected.map(&:last).join("\n") + "\n"
|
|
210
|
+
if direct
|
|
211
|
+
source = source.sub(substitution[0]) { substitution[1] } if substitution
|
|
212
|
+
else
|
|
213
|
+
editor ||= expand_parameters(@variables.fetch("FCEDIT", ""))
|
|
214
|
+
editor = "ed" if editor.empty?
|
|
215
|
+
result, source = edit_history(source, editor, streams)
|
|
216
|
+
return result unless result.zero?
|
|
217
|
+
end
|
|
218
|
+
raise ExecutionError, "fc nesting exceeds 100 levels" if @eval_depth >= 100
|
|
219
|
+
@eval_depth += 1
|
|
220
|
+
begin
|
|
221
|
+
history.add(source)
|
|
222
|
+
evaluate_program(StringIO.new(source), streams)
|
|
223
|
+
ensure
|
|
224
|
+
@eval_depth -= 1
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def edit_history(source, editor, streams)
|
|
229
|
+
Dir.mktmpdir("kwsh-fc-") do |directory|
|
|
230
|
+
path = File.join(directory, "command")
|
|
231
|
+
File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0600) { |file| file.chmod(0600); file.write(source) }
|
|
232
|
+
result = external([editor, path], streams)
|
|
233
|
+
return [result, nil] unless result.zero?
|
|
234
|
+
File.open(path, File::RDONLY | File::NOFOLLOW | File::NONBLOCK) do |file|
|
|
235
|
+
raise ExecutionError, "fc: editor did not leave a private regular file" unless file.stat.file? && file.stat.uid == Process.uid && file.stat.nlink == 1 && (file.stat.mode & 077) == 0
|
|
236
|
+
[0, file.read]
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
require_relative "native_completion"
|
|
2
|
+
require_relative "native_prompt_format"
|
|
3
|
+
require_relative "../terminal/ansi"
|
|
4
|
+
|
|
5
|
+
module Kward
|
|
6
|
+
module NativeShell
|
|
7
|
+
# Prompt-time state only. Hosted shells keep their composer and notifications.
|
|
8
|
+
module Interactive
|
|
9
|
+
class AgentCommandTimeout < StandardError; end
|
|
10
|
+
|
|
11
|
+
def run_shell_agent_command(command, timeout:, output:, error:)
|
|
12
|
+
require "timeout"
|
|
13
|
+
@shell_agent_command = true
|
|
14
|
+
previous = Signal.trap("INT") { raise Interrupt }
|
|
15
|
+
File.open(File::NULL) do |input|
|
|
16
|
+
Timeout.timeout(timeout, AgentCommandTimeout) do
|
|
17
|
+
yield
|
|
18
|
+
with_pager_suppressed { run(command, input: input, output: output, error: error) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
rescue AgentCommandTimeout
|
|
22
|
+
error.puts("kwsh: shell agent command timed out")
|
|
23
|
+
@status = 124
|
|
24
|
+
rescue Interrupt
|
|
25
|
+
@status = 130
|
|
26
|
+
ensure
|
|
27
|
+
Signal.trap("INT", previous) if previous
|
|
28
|
+
@shell_agent_command = false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def option_enabled?(name)
|
|
32
|
+
@options.include?(name)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def complete_line(text, cursor, cancel: nil)
|
|
36
|
+
return if @hosted
|
|
37
|
+
|
|
38
|
+
commands = self.class::BUILTINS + @aliases.keys + @functions.keys
|
|
39
|
+
Completion.new(directory: @directory, variables: @variables, commands: commands).complete(text, cursor) do |words|
|
|
40
|
+
programmed_candidates(words, cancel: cancel) unless @job_child || @history&.paused?
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def vi_expansions(text, full:)
|
|
45
|
+
previous_streams, previous_substitutions = @streams, @process_substitutions
|
|
46
|
+
previous_status = @status
|
|
47
|
+
@process_substitutions = []
|
|
48
|
+
pattern = text.match?(/[*?\[]/) ? text : text + "*"
|
|
49
|
+
@streams = @base_streams || { 0 => $stdin, 1 => $stdout, 2 => $stderr }
|
|
50
|
+
matches = if full
|
|
51
|
+
tokens = Lexer.new(pattern).tokens
|
|
52
|
+
raise ExecutionError, "vi expansion requires one shell word" unless tokens.one? && tokens.first.value.is_a?(Word)
|
|
53
|
+
expand(tokens.first.value)
|
|
54
|
+
else
|
|
55
|
+
Locale.with(@variables) { |locale| Glob.new(pattern, cwd: @directory, locale: locale, globstar: @globstar).matches }
|
|
56
|
+
end
|
|
57
|
+
matches.map { |path| File.directory?(@directory.resolve(path)) && !path.end_with?("/") ? path + "/" : path }
|
|
58
|
+
ensure
|
|
59
|
+
resources = @process_substitutions
|
|
60
|
+
@streams, @process_substitutions = previous_streams, previous_substitutions
|
|
61
|
+
@status = previous_status
|
|
62
|
+
finish_substitutions(resources) if resources
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def vi_input_error(exception, error:)
|
|
66
|
+
@options.delete("vi")
|
|
67
|
+
@status = 2
|
|
68
|
+
error.puts("kwsh: vi disabled: #{exception.message}")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def vi_edit(source, input:, output:, error:)
|
|
72
|
+
result, text = edit_history(source, "vi", { 0 => input, 1 => output, 2 => error }.merge(@persistent_descriptors))
|
|
73
|
+
@status = result
|
|
74
|
+
text if result.zero?
|
|
75
|
+
rescue ExecutionError, SystemCallError, IOError, ArgumentError => e
|
|
76
|
+
error.puts("kwsh: vi: #{e.message}")
|
|
77
|
+
@status = 2
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def prompt_text(name, error:, formatted: false)
|
|
82
|
+
default = { "PS1" => "$ ", "PS2" => "> ", "PS4" => "+ " }.fetch(name)
|
|
83
|
+
prefix = formatted && name != "PS4" && history.paused? ? "[private] " : ""
|
|
84
|
+
if formatted && name == "PS1" && @variables.key?("PROMPT")
|
|
85
|
+
color = error.tty? && @variables["TERM"] != "dumb" && ANSI.enabled?(error, env: @variables)
|
|
86
|
+
return PromptFormat.new(@variables.fetch("PROMPT"), directory: cwd, home: @variables["HOME"], status: @status, color: color, prefix: prefix)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
value = expand_parameters(@variables.fetch(name, default))
|
|
90
|
+
value = value.gsub(/!!|!/) { |mark| mark == "!!" ? "!" : history.next_number.to_s } if name == "PS1" && !@hosted
|
|
91
|
+
prefix + value
|
|
92
|
+
rescue SyntaxError, ExpansionError, ExecutionError => e
|
|
93
|
+
error&.puts("kwsh: #{name}: #{e.message}")
|
|
94
|
+
prefix.to_s + default
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def check_mail(error, now: Process.clock_gettime(Process::CLOCK_MONOTONIC))
|
|
98
|
+
return if @hosted
|
|
99
|
+
@mail ||= MailNotifications.new
|
|
100
|
+
@mail.check(@variables, @directory, now: now) do |message|
|
|
101
|
+
error&.puts(expand_parameters(message))
|
|
102
|
+
rescue SyntaxError, ExpansionError, ExecutionError => e
|
|
103
|
+
error&.puts("kwsh: MAILPATH: #{e.message}")
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def trace(text, streams)
|
|
110
|
+
return unless @options.include?("xtrace")
|
|
111
|
+
streams[2]&.puts("#{prompt_text('PS4', error: streams[2])}#{text}")
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
class MailNotifications
|
|
116
|
+
def initialize
|
|
117
|
+
@seen = {}
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def check(variables, directory, now:)
|
|
121
|
+
interval = variables.fetch("MAILCHECK", "600")
|
|
122
|
+
interval = interval.match?(/\A[0-9]+\z/) ? interval.to_i : 600
|
|
123
|
+
return if @checked && now - @checked < interval
|
|
124
|
+
@checked = now
|
|
125
|
+
paths = variables.key?("MAILPATH") ? variables["MAILPATH"].split(":") : [variables["MAIL"]].compact
|
|
126
|
+
current = {}
|
|
127
|
+
paths.each do |entry|
|
|
128
|
+
path, message = parse(entry)
|
|
129
|
+
next if path.empty?
|
|
130
|
+
key = nil
|
|
131
|
+
begin
|
|
132
|
+
key = directory.resolve(path)
|
|
133
|
+
modified = File.stat(key).mtime
|
|
134
|
+
rescue SystemCallError, IOError, ArgumentError
|
|
135
|
+
key ||= path
|
|
136
|
+
modified = nil
|
|
137
|
+
end
|
|
138
|
+
yield(message || "you have mail") if @seen.key?(key) && modified && modified != @seen[key]
|
|
139
|
+
current[key] = modified
|
|
140
|
+
end
|
|
141
|
+
@seen = current
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def parse(entry)
|
|
147
|
+
path = +""
|
|
148
|
+
index = 0
|
|
149
|
+
while index < entry.length
|
|
150
|
+
if entry[index, 2] == '\\%'
|
|
151
|
+
path << "%"
|
|
152
|
+
index += 2
|
|
153
|
+
elsif entry[index] == "%"
|
|
154
|
+
return [path, entry[(index + 1)..]]
|
|
155
|
+
else
|
|
156
|
+
path << entry[index]
|
|
157
|
+
index += 1
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
[path, nil]
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Kward
|
|
2
|
+
module NativeShell
|
|
3
|
+
Invocation = Struct.new(:options, :arguments, :file, :command, :command_name, :interactive, :help, keyword_init: true) do
|
|
4
|
+
def self.parse(arguments)
|
|
5
|
+
args = arguments.dup
|
|
6
|
+
options = []
|
|
7
|
+
mode = nil
|
|
8
|
+
interactive = false
|
|
9
|
+
while args.first&.start_with?("-", "+") && args.first != "-"
|
|
10
|
+
option = args.shift
|
|
11
|
+
break if option == "--"
|
|
12
|
+
if option == "--help"
|
|
13
|
+
return new(help: true)
|
|
14
|
+
end
|
|
15
|
+
option[1..].each_char do |flag|
|
|
16
|
+
case flag
|
|
17
|
+
when "c", "s"
|
|
18
|
+
raise ArgumentError, "-c and -s are mutually exclusive" if mode && mode != flag
|
|
19
|
+
raise ArgumentError, "+#{flag} is not supported" if option.start_with?("+")
|
|
20
|
+
mode = flag
|
|
21
|
+
when "i" then interactive = option.start_with?("-")
|
|
22
|
+
when "o"
|
|
23
|
+
raise ArgumentError, "#{option} requires an option name" if args.empty?
|
|
24
|
+
options.concat(["#{option[0]}o", args.shift])
|
|
25
|
+
else options << "#{option[0]}#{flag}"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
args.shift if args.first == "-"
|
|
30
|
+
command = args.shift if mode == "c"
|
|
31
|
+
raise ArgumentError, "-c requires a command" if mode == "c" && !command
|
|
32
|
+
file = args.shift unless mode || args.empty?
|
|
33
|
+
command_name = args.shift if mode == "c"
|
|
34
|
+
new(options: options, arguments: args, file: file, command: command, command_name: command_name, interactive: interactive)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def prompt?(input, error)
|
|
38
|
+
!help && !command && !file && input.tty? && error.tty?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
require "io/console"
|
|
2
|
+
|
|
3
|
+
module Kward
|
|
4
|
+
module NativeShell
|
|
5
|
+
# Each job has an owned process group. Only its interpreter leader is reaped
|
|
6
|
+
# here; that interpreter owns the pipeline and substitution children.
|
|
7
|
+
module Jobs
|
|
8
|
+
Job = Struct.new(:id, :pid, :group, :state, :status, :command, :terminal_mode, :notified, keyword_init: true)
|
|
9
|
+
MAX_JOBS = 1_024
|
|
10
|
+
TCSETPGRP = Fiddle::Function.new(Fiddle::Handle::DEFAULT["tcsetpgrp"], [Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
|
|
11
|
+
|
|
12
|
+
def notify_jobs(error:, force: false)
|
|
13
|
+
return unless interactive_scope? && (force || @options.include?("notify"))
|
|
14
|
+
reap_jobs
|
|
15
|
+
@jobs.each_value do |job|
|
|
16
|
+
next if job.state == :running || job.notified == job.state
|
|
17
|
+
error&.puts("[#{job.id}] #{job.state.to_s.capitalize} #{job.command}")
|
|
18
|
+
job.notified = job.state
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def close
|
|
23
|
+
active = @jobs.values.reject { |job| job.state == :done }
|
|
24
|
+
active.each do |job|
|
|
25
|
+
signal_job(job, "HUP")
|
|
26
|
+
signal_job(job, "CONT")
|
|
27
|
+
signal_job(job, "KILL")
|
|
28
|
+
end
|
|
29
|
+
active.each do |job|
|
|
30
|
+
begin
|
|
31
|
+
Process.waitpid(job.pid)
|
|
32
|
+
rescue Errno::ECHILD
|
|
33
|
+
# A completed job may already have been collected by its waiter.
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
@jobs.clear
|
|
37
|
+
ensure
|
|
38
|
+
close_resources
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def foreground_process?(node)
|
|
44
|
+
commands = node.values.first
|
|
45
|
+
commands.length > 1 || commands.first.kind == :subshell
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def job_description(tree)
|
|
49
|
+
return tree.source if tree.source
|
|
50
|
+
return job_description(tree.values.first) if tree.kind == :and_or
|
|
51
|
+
tree.kind.to_s
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def start_job(tree, streams, background:)
|
|
55
|
+
reap_jobs
|
|
56
|
+
raise ExecutionError, "too many jobs (maximum #{MAX_JOBS}); use wait" if @jobs.size >= MAX_JOBS
|
|
57
|
+
reader, writer = IO.pipe
|
|
58
|
+
null = File.open(File::NULL) if background && !@options.include?("monitor")
|
|
59
|
+
selected = null ? streams.merge(0 => null) : streams
|
|
60
|
+
group = !@job_child
|
|
61
|
+
pid = fork_program(tree, selected, close: [writer], group: group, gate: reader,
|
|
62
|
+
asynchronous: background && !@options.include?("monitor"))
|
|
63
|
+
reader.close
|
|
64
|
+
Process.setpgid(pid, pid) if group
|
|
65
|
+
id = (@jobs.keys.max || 0) + 1
|
|
66
|
+
job = Job.new(id: id, pid: pid, group: group, state: :running, command: job_description(tree))
|
|
67
|
+
@jobs[job.id] = job
|
|
68
|
+
if background
|
|
69
|
+
@last_background_pid = pid
|
|
70
|
+
writer.close
|
|
71
|
+
0
|
|
72
|
+
else
|
|
73
|
+
foreground_job(job, streams, gate: writer)
|
|
74
|
+
end
|
|
75
|
+
ensure
|
|
76
|
+
reader&.close unless reader&.closed?
|
|
77
|
+
writer&.close unless writer&.closed?
|
|
78
|
+
null&.close
|
|
79
|
+
terminate(pid) if pid && !job
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def signal_job(job, signal)
|
|
83
|
+
Process.kill(signal == "EXIT" ? 0 : signal, job.group ? -job.pid : job.pid)
|
|
84
|
+
job.state = :running if signal == "CONT"
|
|
85
|
+
rescue Errno::ESRCH
|
|
86
|
+
nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def update_job(job, status)
|
|
90
|
+
if status.stopped?
|
|
91
|
+
job.state = :stopped
|
|
92
|
+
job.status = 128 + status.stopsig
|
|
93
|
+
else
|
|
94
|
+
job.state = :done
|
|
95
|
+
job.status = status.exitstatus || 128 + status.termsig
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def reap_jobs
|
|
100
|
+
@jobs.each_value do |job|
|
|
101
|
+
next if job.state == :done
|
|
102
|
+
result = Process.waitpid2(job.pid, Process::WNOHANG | Process::WUNTRACED)
|
|
103
|
+
update_job(job, result.last) if result
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def find_job(value)
|
|
108
|
+
reap_jobs
|
|
109
|
+
if %w[% %% %+].include?(value)
|
|
110
|
+
@jobs.values.last
|
|
111
|
+
elsif value == "%-"
|
|
112
|
+
@jobs.values[-2]
|
|
113
|
+
elsif value.match?(/\A%[0-9]+\z/)
|
|
114
|
+
@jobs[value[1..].to_i]
|
|
115
|
+
elsif value.match?(/\A[0-9]+\z/)
|
|
116
|
+
@jobs.values.find { |job| job.pid == value.to_i }
|
|
117
|
+
elsif value.start_with?("%")
|
|
118
|
+
matches = @jobs.values.select do |job|
|
|
119
|
+
value.start_with?("%?") ? job.command.include?(value[2..]) : job.command.start_with?(value[1..])
|
|
120
|
+
end
|
|
121
|
+
raise ExecutionError, "ambiguous job: #{value}" if matches.length > 1
|
|
122
|
+
matches.first
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def wait_job(job, streams, stopped: false, interruptible: false)
|
|
127
|
+
until job.state == :done || stopped && job.state == :stopped
|
|
128
|
+
if interruptible && !@pending_signals.empty?
|
|
129
|
+
# Evaluation records wait's interrupted status before running the
|
|
130
|
+
# deferred action, so the trap observes the status of wait itself.
|
|
131
|
+
return 128 + Signal.list.fetch(@pending_signals.first)
|
|
132
|
+
end
|
|
133
|
+
result = Process.waitpid2(job.pid, Process::WNOHANG | Process::WUNTRACED)
|
|
134
|
+
result ? update_job(job, result.last) : sleep(0.01)
|
|
135
|
+
end
|
|
136
|
+
@jobs.delete(job.id) if job.state == :done
|
|
137
|
+
job.status
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def foreground_job(job, streams, gate: nil)
|
|
141
|
+
# A command may redirect stdin and still open /dev/tty (for example less).
|
|
142
|
+
terminal = [streams[0], $stdin].compact.find(&:tty?) if job.group && !@shell_agent_command
|
|
143
|
+
previous_ttou = Signal.trap("TTOU", "IGNORE") if terminal
|
|
144
|
+
shell_mode = terminal.console_mode if terminal
|
|
145
|
+
if terminal && TCSETPGRP.call(terminal.fileno, job.pid) < 0
|
|
146
|
+
raise SystemCallError.new("tcsetpgrp", Fiddle.last_error)
|
|
147
|
+
end
|
|
148
|
+
terminal.console_mode = job.terminal_mode if terminal && job.terminal_mode
|
|
149
|
+
gate&.close
|
|
150
|
+
if job.state == :stopped
|
|
151
|
+
job.state = :running
|
|
152
|
+
signal_job(job, "CONT")
|
|
153
|
+
end
|
|
154
|
+
result = wait_job(job, streams, stopped: true)
|
|
155
|
+
job.terminal_mode = terminal.console_mode if terminal && job.state == :stopped
|
|
156
|
+
@pending_signals << "INT" if result == 130 && @traps.key?("INT")
|
|
157
|
+
result
|
|
158
|
+
rescue Interrupt, Interactive::AgentCommandTimeout
|
|
159
|
+
if @shell_agent_command
|
|
160
|
+
signal_job(job, "KILL")
|
|
161
|
+
Process.waitpid(job.pid) rescue nil
|
|
162
|
+
@jobs.delete(job.id)
|
|
163
|
+
else
|
|
164
|
+
signal_job(job, "INT")
|
|
165
|
+
end
|
|
166
|
+
raise
|
|
167
|
+
ensure
|
|
168
|
+
if terminal
|
|
169
|
+
begin
|
|
170
|
+
TCSETPGRP.call(terminal.fileno, Process.getpgrp)
|
|
171
|
+
terminal.console_mode = shell_mode if shell_mode && (job.state == :stopped || !result || result >= 128)
|
|
172
|
+
ensure
|
|
173
|
+
Signal.trap("TTOU", previous_ttou)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def jobs_builtin(args, streams)
|
|
179
|
+
flags = builtin_flags(args, "lprs", "jobs")
|
|
180
|
+
mode = flags.reverse.find { |flag| %w[l p].include?(flag) }
|
|
181
|
+
states = flags & %w[r s]
|
|
182
|
+
reap_jobs
|
|
183
|
+
jobs = args.empty? ? @jobs.values : args.map { |arg| find_job(arg) || raise(ExecutionError, "jobs: no such job: #{arg}") }
|
|
184
|
+
jobs.each do |job|
|
|
185
|
+
next if states == ["r"] && job.state != :running || states == ["s"] && job.state != :stopped
|
|
186
|
+
text = mode == "p" ? job.pid.to_s : "[#{job.id}] #{mode == 'l' ? "#{job.pid} " : ''}#{job.state.to_s.capitalize} #{job.command}"
|
|
187
|
+
write_line(streams, text)
|
|
188
|
+
end
|
|
189
|
+
0
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def wait_builtin(args, streams)
|
|
193
|
+
args.shift if args.first == "--"
|
|
194
|
+
raise ExecutionError, "wait: unsupported option" if args.any? { |arg| arg.start_with?("-") }
|
|
195
|
+
all = args.empty?
|
|
196
|
+
targets = all ? @jobs.values.dup : args.map { |arg| find_job(arg) }
|
|
197
|
+
result = 0
|
|
198
|
+
targets.each do |job|
|
|
199
|
+
result = job ? wait_job(job, streams, interruptible: true) : 127
|
|
200
|
+
return result if job && @jobs.key?(job.id)
|
|
201
|
+
end
|
|
202
|
+
all ? 0 : result
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def resume_job(args, streams, foreground:)
|
|
206
|
+
raise ExecutionError, "fg: needs a user-driven terminal" if foreground && @shell_agent_command
|
|
207
|
+
|
|
208
|
+
args.shift if args.first == "--"
|
|
209
|
+
raise ExecutionError, "job control is disabled; use set -m" unless @options.include?("monitor")
|
|
210
|
+
raise ExecutionError, "expected at most one job ID" if args.length > 1
|
|
211
|
+
job = find_job(args.first || "%+")
|
|
212
|
+
raise ExecutionError, "no such active job" unless job && job.state != :done
|
|
213
|
+
return foreground_job(job, streams) if foreground
|
|
214
|
+
signal_job(job, "CONT")
|
|
215
|
+
job.state = :running
|
|
216
|
+
0
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def kill_builtin(args, streams)
|
|
220
|
+
signal = "TERM"
|
|
221
|
+
if args.first == "-l"
|
|
222
|
+
args.shift
|
|
223
|
+
text = args.empty? ? Signal.list.keys.reject { |name| name == "EXIT" }.join(" ") : args.map { |arg| arg.match?(/\A[0-9]+\z/) ? signal_name(arg.to_i > 128 ? (arg.to_i - 128).to_s : arg) : Signal.list.fetch(signal_name(arg)).to_s }.join(" ")
|
|
224
|
+
write_line(streams, text)
|
|
225
|
+
return 0
|
|
226
|
+
elsif %w[-s -n].include?(args.first)
|
|
227
|
+
args.shift
|
|
228
|
+
signal = signal_name(args.shift&.upcase)
|
|
229
|
+
elsif args.first&.start_with?("-") && args.first != "--"
|
|
230
|
+
signal = signal_name(args.shift[1..].upcase)
|
|
231
|
+
end
|
|
232
|
+
args.shift if args.first == "--"
|
|
233
|
+
raise ExecutionError, "kill: expected a pid or job ID" if args.empty?
|
|
234
|
+
args.each do |target|
|
|
235
|
+
if target.start_with?("%")
|
|
236
|
+
job = find_job(target)
|
|
237
|
+
raise ExecutionError, "kill: no such active job: #{target}" unless job && job.state != :done
|
|
238
|
+
signal_job(job, signal)
|
|
239
|
+
else
|
|
240
|
+
unless target.match?(/\A-?[0-9]{1,10}\z/) && target.to_i.abs <= 2_147_483_647
|
|
241
|
+
raise ExecutionError, "kill: invalid pid: #{target}"
|
|
242
|
+
end
|
|
243
|
+
Process.kill(signal == "EXIT" ? 0 : signal, target.to_i)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
0
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|