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.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +130 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/Gemfile.lock +6 -2
  5. data/README.md +3 -3
  6. data/Rakefile +3 -0
  7. data/doc/agent-tools.md +11 -0
  8. data/doc/api.md +4 -1
  9. data/doc/composer.md +4 -3
  10. data/doc/configuration.md +60 -11
  11. data/doc/extensibility.md +2 -0
  12. data/doc/getting-started.md +3 -3
  13. data/doc/git.md +1 -1
  14. data/doc/memory.md +1 -1
  15. data/doc/plugins.md +40 -5
  16. data/doc/providers.md +10 -4
  17. data/doc/rpc.md +125 -7
  18. data/doc/session-management.md +36 -1
  19. data/doc/shell.md +643 -27
  20. data/doc/tabs.md +2 -1
  21. data/doc/troubleshooting.md +1 -1
  22. data/doc/usage.md +21 -11
  23. data/examples/plugins/openrouter_proxy/README.md +103 -0
  24. data/examples/plugins/openrouter_proxy/fake_embeddings.rb +85 -0
  25. data/examples/plugins/openrouter_proxy/plugin.rb +7 -0
  26. data/examples/plugins/openrouter_proxy/protocol.rb +202 -0
  27. data/examples/plugins/openrouter_proxy/server.rb +186 -0
  28. data/examples/plugins/space_invaders.rb +0 -1
  29. data/exe/kward +2 -2
  30. data/exe/kwsh +7 -0
  31. data/kward.gemspec +3 -1
  32. data/lib/kward/agent.rb +43 -9
  33. data/lib/kward/cli/commands.rb +41 -11
  34. data/lib/kward/cli/interactive_turn.rb +10 -0
  35. data/lib/kward/cli/plugins.rb +5 -1
  36. data/lib/kward/cli/prompt_interface.rb +4 -1
  37. data/lib/kward/cli/rendering.rb +21 -28
  38. data/lib/kward/cli/runtime_helpers.rb +52 -34
  39. data/lib/kward/cli/settings/model.rb +1 -12
  40. data/lib/kward/cli/tabs.rb +91 -18
  41. data/lib/kward/cli/tool_summaries.rb +2 -0
  42. data/lib/kward/cli/transcript_formatter.rb +3 -10
  43. data/lib/kward/cli/worktrees.rb +1 -0
  44. data/lib/kward/cli.rb +56 -7
  45. data/lib/kward/config/core.rb +16 -5
  46. data/lib/kward/config/extensions.rb +3 -0
  47. data/lib/kward/config/paths.rb +11 -0
  48. data/lib/kward/config/settings.rb +7 -0
  49. data/lib/kward/conversation.rb +11 -1
  50. data/lib/kward/entrypoint.rb +14 -0
  51. data/lib/kward/events.rb +2 -0
  52. data/lib/kward/generated_images.rb +237 -0
  53. data/lib/kward/model/catalog.rb +6 -3
  54. data/lib/kward/model/client.rb +105 -36
  55. data/lib/kward/model/codex_images.rb +83 -0
  56. data/lib/kward/model/copilot_models.rb +8 -4
  57. data/lib/kward/model/model_info.rb +27 -9
  58. data/lib/kward/model/openrouter_model_cache.rb +4 -2
  59. data/lib/kward/model/payloads.rb +6 -5
  60. data/lib/kward/model/sources.rb +3 -1
  61. data/lib/kward/model/stream_parser.rb +4 -7
  62. data/lib/kward/pan/index.html.erb +8 -1
  63. data/lib/kward/pan/server.rb +13 -1
  64. data/lib/kward/plugins/codex_responses.rb +73 -0
  65. data/lib/kward/plugins/host.rb +16 -0
  66. data/lib/kward/plugins/http_server.rb +83 -0
  67. data/lib/kward/plugins/registry.rb +39 -0
  68. data/lib/kward/prompt_interface/composer_controller.rb +0 -1
  69. data/lib/kward/prompt_interface/composer_renderer.rb +159 -72
  70. data/lib/kward/prompt_interface/editor/controller.rb +1 -1
  71. data/lib/kward/prompt_interface/editor/renderer.rb +10 -7
  72. data/lib/kward/prompt_interface/file_overlay.rb +3 -3
  73. data/lib/kward/prompt_interface/generated_image_preview.rb +162 -0
  74. data/lib/kward/prompt_interface/git_prompt.rb +18 -11
  75. data/lib/kward/prompt_interface/interactive/renderer.rb +1 -1
  76. data/lib/kward/prompt_interface/interactive/state.rb +18 -20
  77. data/lib/kward/prompt_interface/key_handler.rb +108 -25
  78. data/lib/kward/prompt_interface/layout.rb +11 -1
  79. data/lib/kward/prompt_interface/overlay_renderer.rb +25 -8
  80. data/lib/kward/prompt_interface/project_browser.rb +27 -54
  81. data/lib/kward/prompt_interface/prompt_renderer.rb +27 -35
  82. data/lib/kward/prompt_interface/question_prompt.rb +19 -28
  83. data/lib/kward/prompt_interface/runtime_state.rb +31 -9
  84. data/lib/kward/prompt_interface/screen.rb +24 -21
  85. data/lib/kward/prompt_interface/selection_prompt.rb +8 -8
  86. data/lib/kward/prompt_interface/slash_overlay.rb +1 -1
  87. data/lib/kward/prompt_interface/transcript_renderer.rb +7 -6
  88. data/lib/kward/prompt_interface.rb +109 -39
  89. data/lib/kward/prompts.rb +7 -1
  90. data/lib/kward/pty/adaptive_output_sink.rb +38 -4
  91. data/lib/kward/pty/interactive_runner.rb +20 -28
  92. data/lib/kward/pty/transcript_normalizer.rb +65 -21
  93. data/lib/kward/rpc/config_manager.rb +3 -0
  94. data/lib/kward/rpc/server.rb +27 -5
  95. data/lib/kward/rpc/session_manager.rb +62 -17
  96. data/lib/kward/rpc/transcript_normalizer.rb +11 -7
  97. data/lib/kward/sessions/store.rb +30 -3
  98. data/lib/kward/shell/kwsh.rb +27 -7
  99. data/lib/kward/shell/kwshrc.rb +11 -2
  100. data/lib/kward/shell/native_arithmetic.rb +255 -0
  101. data/lib/kward/shell/native_arrays.rb +102 -0
  102. data/lib/kward/shell/native_braces.rb +96 -0
  103. data/lib/kward/shell/native_builtins.rb +476 -0
  104. data/lib/kward/shell/native_completion.rb +169 -0
  105. data/lib/kward/shell/native_condition.rb +75 -0
  106. data/lib/kward/shell/native_directory.rb +116 -0
  107. data/lib/kward/shell/native_exec_state.rb +73 -0
  108. data/lib/kward/shell/native_expansion.rb +252 -0
  109. data/lib/kward/shell/native_fields.rb +62 -0
  110. data/lib/kward/shell/native_glob.rb +96 -0
  111. data/lib/kward/shell/native_history.rb +242 -0
  112. data/lib/kward/shell/native_interactive.rb +164 -0
  113. data/lib/kward/shell/native_invocation.rb +42 -0
  114. data/lib/kward/shell/native_jobs.rb +250 -0
  115. data/lib/kward/shell/native_line_editor.rb +382 -0
  116. data/lib/kward/shell/native_line_terminal.rb +286 -0
  117. data/lib/kward/shell/native_locale.rb +109 -0
  118. data/lib/kward/shell/native_locals.rb +57 -0
  119. data/lib/kward/shell/native_pattern.rb +200 -0
  120. data/lib/kward/shell/native_process.rb +279 -0
  121. data/lib/kward/shell/native_program.rb +40 -0
  122. data/lib/kward/shell/native_programmable_completion.rb +115 -0
  123. data/lib/kward/shell/native_prompt_format.rb +89 -0
  124. data/lib/kward/shell/native_runtime.rb +964 -0
  125. data/lib/kward/shell/native_substitution.rb +110 -0
  126. data/lib/kward/shell/native_syntax.rb +946 -0
  127. data/lib/kward/shell/native_traps.rb +196 -0
  128. data/lib/kward/shell/native_vi.rb +407 -0
  129. data/lib/kward/shell/native_vi_terminal.rb +19 -0
  130. data/lib/kward/shell/native_worker.rb +82 -0
  131. data/lib/kward/shell/persistent_session.rb +184 -190
  132. data/lib/kward/shell/prompt.rb +14 -0
  133. data/lib/kward/shell/prompt_agent.rb +17 -0
  134. data/lib/kward/shell/script_runner.rb +244 -0
  135. data/lib/kward/shell/standalone_agent.rb +31 -0
  136. data/lib/kward/shell/standalone_link.rb +78 -0
  137. data/lib/kward/shell/standalone_terminal.rb +287 -0
  138. data/lib/kward/terminal/keys.rb +22 -1
  139. data/lib/kward/terminal/locale_text.rb +29 -0
  140. data/lib/kward/terminal/sequences.rb +45 -4
  141. data/lib/kward/tools/datetime.rb +85 -0
  142. data/lib/kward/tools/generate_image.rb +36 -0
  143. data/lib/kward/tools/registry.rb +35 -9
  144. data/lib/kward/tools/tool_call.rb +3 -1
  145. data/lib/kward/transcripts/markdown_transcript.rb +8 -0
  146. data/lib/kward/transcripts/turn_summary_formatter.rb +93 -0
  147. data/lib/kward/transport/gateway.rb +2 -1
  148. data/lib/kward/turn_summary.rb +68 -0
  149. data/lib/kward/version.rb +1 -1
  150. data/lib/main.rb +6 -3
  151. metadata +84 -1
@@ -0,0 +1,75 @@
1
+ module Kward
2
+ module NativeShell
3
+ module Condition
4
+ UNARY = %w[-n -z -e -a -f -d -s -r -w -x -L -h -b -c -p -S -t -v].freeze
5
+ BINARY = %w[= == != =~ < > -eq -ne -lt -le -gt -ge -nt -ot -ef].freeze
6
+
7
+ private
8
+
9
+ def condition_value(tree)
10
+ kind, *values = tree
11
+ case kind
12
+ when :and then condition_value(values[0]) && condition_value(values[1])
13
+ when :or then condition_value(values[0]) || condition_value(values[1])
14
+ when :not then !condition_value(values.first)
15
+ when :string then !scalar(values.first).empty?
16
+ when :unary
17
+ operator, word = values
18
+ value = scalar(word)
19
+ path = @directory.resolve(value) unless %w[-n -z -v -t].include?(operator)
20
+ case operator
21
+ when "-n" then !value.empty?
22
+ when "-z" then value.empty?
23
+ when "-v" then parameter_set?(value)
24
+ when "-t" then @streams[value.to_i]&.tty? || false
25
+ when "-e", "-a" then File.exist?(path)
26
+ when "-f" then File.file?(path)
27
+ when "-d" then File.directory?(path)
28
+ when "-s" then File.size?(path).to_i.positive?
29
+ when "-r" then File.readable?(path)
30
+ when "-w" then File.writable?(path)
31
+ when "-x" then File.executable?(path)
32
+ when "-L", "-h" then File.symlink?(path)
33
+ when "-b" then File.blockdev?(path)
34
+ when "-c" then File.chardev?(path)
35
+ when "-p" then File.pipe?(path)
36
+ when "-S" then File.socket?(path)
37
+ end
38
+ when :binary
39
+ operator, left, right = values
40
+ value = scalar(left)
41
+ if %w[= == !=].include?(operator)
42
+ match = matches_pattern?(pattern(right), value)
43
+ return operator == "!=" ? !match : match
44
+ end
45
+ if operator == "=~"
46
+ expression = word_fields(right).flatten.map { |part| part.quoted ? Regexp.escape(part.text) : part.text }.join
47
+ match = Regexp.new(expression, timeout: 0.1).match(value)
48
+ captures = match ? match.to_a.each_with_index.to_h { |capture, index| [index, capture.to_s] } : {}
49
+ @arrays["BASH_REMATCH"] = Arrays::ArrayValue.new(associative: false, values: captures)
50
+ return !!match
51
+ end
52
+ other = scalar(right)
53
+ case operator
54
+ when "<" then Locale.with(@variables) { |locale| locale.compare(value, other).negative? }
55
+ when ">" then Locale.with(@variables) { |locale| locale.compare(value, other).positive? }
56
+ when "-eq", "-ne", "-lt", "-le", "-gt", "-ge"
57
+ operators = { "-eq" => "==", "-ne" => "!=", "-lt" => "<", "-le" => "<=", "-gt" => ">", "-ge" => ">=" }
58
+ Arithmetic.binary(operators.fetch(operator), arithmetic_value(value).to_i, arithmetic_value(other).to_i) == 1
59
+ when "-nt", "-ot", "-ef"
60
+ first, second = [value, other].map { |name| @directory.resolve(name) }
61
+ if operator == "-ef"
62
+ File.exist?(first) && File.exist?(second) && File.identical?(first, second)
63
+ elsif operator == "-nt"
64
+ File.exist?(first) && (!File.exist?(second) || File.mtime(first) > File.mtime(second))
65
+ else
66
+ File.exist?(second) && (!File.exist?(first) || File.mtime(first) < File.mtime(second))
67
+ end
68
+ end
69
+ end
70
+ rescue RegexpError, Regexp::TimeoutError => e
71
+ raise ExecutionError, "conditional regular expression: #{e.message}"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,116 @@
1
+ require "fiddle"
2
+ require "fcntl"
3
+
4
+ module Kward
5
+ module NativeShell
6
+ # Owns directory identity independently of the shell's logical PWD. Never
7
+ # changes the host cwd; only an exec-bound child may enter this descriptor.
8
+ class Directory
9
+ OPENAT = Fiddle::Function.new(Fiddle::Handle::DEFAULT["openat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_VARIADIC], Fiddle::TYPE_INT)
10
+ FACCESSAT = Fiddle::Function.new(Fiddle::Handle::DEFAULT["faccessat"], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
11
+ FCHDIR = Fiddle::Function.new(Fiddle::Handle::DEFAULT["fchdir"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
12
+
13
+ # Darwin sys/fcntl.h: O_SEARCH | O_CLOEXEC, AT_FDCWD, AT_EACCESS.
14
+ # Linux asm-generic/fcntl.h: O_PATH | O_DIRECTORY | O_CLOEXEC. O_PATH
15
+ # needs a separate effective-ID search-permission check on the directory.
16
+ FLAGS, CLOEXEC, AT_FDCWD, AT_EACCESS = if RUBY_PLATFORM.include?("darwin")
17
+ [0x41100000, 0x01000000, -2, 0x10]
18
+ elsif RUBY_PLATFORM.include?("linux")
19
+ [0x290000, 0x80000, -100, 0x200]
20
+ else
21
+ raise LoadError, "native directory handles require macOS or Linux"
22
+ end
23
+
24
+ def initialize(path, base: nil)
25
+ raise ArgumentError, "NUL bytes are not allowed in directory paths" if path.include?("\0")
26
+ fd = OPENAT.call(base ? base.fileno : AT_FDCWD, path, FLAGS, Fiddle::TYPE_INT, 0)
27
+ raise SystemCallError.new(path, Fiddle.last_error) if fd < 0
28
+ @handle = IO.for_fd(fd)
29
+ if FACCESSAT.call(fd, ".", 1, AT_EACCESS) < 0
30
+ error = Fiddle.last_error
31
+ close
32
+ raise SystemCallError.new(path, error)
33
+ end
34
+ end
35
+
36
+ def initialize_copy(other)
37
+ super
38
+ @handle = other.handle.dup
39
+ @handle.close_on_exec = true
40
+ end
41
+
42
+ def fileno
43
+ @handle.fileno
44
+ end
45
+
46
+ def close
47
+ @handle.close unless @handle.closed?
48
+ end
49
+
50
+ def closed?
51
+ @handle.closed?
52
+ end
53
+
54
+ def same?(path)
55
+ stat = File.stat(path)
56
+ current = @handle.stat
57
+ stat.dev == current.dev && stat.ino == current.ino
58
+ rescue SystemCallError
59
+ false
60
+ end
61
+
62
+ def physical_path
63
+ File.realpath(access_path)
64
+ end
65
+
66
+ def access_path
67
+ path = if RUBY_PLATFORM.include?("darwin")
68
+ # F_GETPATH uses the kernel's MAXPATHLEN-sized output buffer.
69
+ buffer = "\0" * 1024
70
+ @handle.fcntl(50, buffer)
71
+ buffer.split("\0", 2).first
72
+ else
73
+ File.readlink("/proc/self/fd/#{fileno}")
74
+ end
75
+ raise Errno::ENOENT, "current directory has no pathname" unless same?(path)
76
+ path
77
+ end
78
+
79
+ def resolve(path)
80
+ return path if path.start_with?("/")
81
+ # Do not expand_path here: symlink/.. must be resolved by the kernel.
82
+ File.join(access_path, path.delete_prefix("./"))
83
+ end
84
+
85
+ def open_file(path, mode = "r")
86
+ raise ArgumentError, "NUL bytes are not allowed in file paths" if path.include?("\0")
87
+ flags = mode.is_a?(Integer) ? mode : { "r" => File::RDONLY, "w" => File::WRONLY | File::CREAT | File::TRUNC, "a" => File::WRONLY | File::CREAT | File::APPEND }.fetch(mode)
88
+ fd = OPENAT.call(fileno, path, flags | CLOEXEC, Fiddle::TYPE_INT, 0o666)
89
+ raise SystemCallError.new(path, Fiddle.last_error) if fd < 0
90
+ file = IO.for_fd(fd)
91
+ return file unless block_given?
92
+ begin
93
+ yield file
94
+ ensure
95
+ file.close
96
+ end
97
+ end
98
+
99
+ def protect_descriptors(descriptors)
100
+ return unless descriptors.include?(fileno)
101
+ replacement = IO.for_fd(@handle.fcntl(Fcntl::F_DUPFD, [32, descriptors.max + 1].max))
102
+ replacement.close_on_exec = true
103
+ @handle.close
104
+ @handle = replacement
105
+ end
106
+
107
+ def enter
108
+ raise SystemCallError.new("fchdir", Fiddle.last_error) if FCHDIR.call(fileno) < 0
109
+ end
110
+
111
+ protected
112
+
113
+ attr_reader :handle
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,73 @@
1
+ require_relative "native_directory"
2
+
3
+ module Kward
4
+ module NativeShell
5
+ module NativeProcess
6
+ # A failed standalone interactive exec must return to a live Ruby process.
7
+ # Shell redirections are persisted separately; these backups protect the
8
+ # process's original IOs (including private descriptors), cwd and signals.
9
+ class ExecState
10
+ attr_reader :signals
11
+
12
+ def initialize(descriptors)
13
+ @streams = {}
14
+ @flags = {}
15
+ @signals = {}
16
+ @sync = [STDIN, STDOUT, STDERR].map { |io| io.sync unless io.closed? }
17
+ @mask = File.umask
18
+ @directory = Directory.new(".")
19
+ @directory.protect_descriptors(descriptors)
20
+ descriptors.each do |fd|
21
+ flags = FCNTL.call(fd, Fcntl::F_GETFD, 0)
22
+ if flags < 0
23
+ raise SystemCallError.new("fcntl", Fiddle.last_error) unless Fiddle.last_error == Errno::EBADF::Errno
24
+ @streams[fd] = nil
25
+ next
26
+ end
27
+ copy = FCNTL.call(fd, Fcntl::F_DUPFD, [32, descriptors.max + 1].max)
28
+ raise SystemCallError.new("dup", Fiddle.last_error) if copy < 0
29
+ @streams[fd] = IO.for_fd(copy)
30
+ @streams[fd].close_on_exec = true
31
+ @flags[fd] = [flags, FCNTL.call(fd, Fcntl::F_GETFL, 0)]
32
+ end
33
+ complete = true
34
+ ensure
35
+ close unless complete
36
+ end
37
+
38
+ def restore
39
+ @streams.each do |fd, source|
40
+ if fd < 3
41
+ standard = [STDIN, STDOUT, STDERR][fd]
42
+ if source
43
+ standard.reopen(source)
44
+ standard.sync = @sync[fd]
45
+ else
46
+ standard.close unless standard.closed?
47
+ end
48
+ elsif source
49
+ raise SystemCallError.new("dup2", Fiddle.last_error) if DUP2.call(source.fileno, fd) < 0
50
+ else
51
+ CLOSE.call(fd)
52
+ end
53
+ next unless source
54
+ descriptor_flags, file_flags = @flags.fetch(fd)
55
+ raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFD, descriptor_flags) < 0
56
+ raise SystemCallError.new("fcntl", Fiddle.last_error) if FCNTL.call(fd, Fcntl::F_SETFL, file_flags) < 0
57
+ end
58
+ @directory.enter
59
+ File.umask(@mask)
60
+ @signals.each { |name, handler| Signal.trap(name, handler || "DEFAULT") }
61
+ rescue StandardError => e
62
+ # Continuing with corrupt descriptors could expose private resources.
63
+ raise SystemExit.new(126, "exec recovery failed: #{e.message}")
64
+ end
65
+
66
+ def close
67
+ @streams.each_value { |io| io.close if io && !io.closed? }
68
+ @directory&.close
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,252 @@
1
+ require "etc"
2
+ require_relative "native_fields"
3
+ require_relative "native_pattern"
4
+ require_relative "native_locale"
5
+ require_relative "native_glob"
6
+ require_relative "native_braces"
7
+
8
+ module Kward
9
+ module NativeShell
10
+ class ExpansionError < StandardError; end
11
+
12
+ # Expansions preserve quote provenance. Substitutions execute parsed native
13
+ # trees; expanded values are never reparsed as shell source.
14
+ module Expansion
15
+ private
16
+
17
+ def ifs
18
+ @variables.fetch("IFS", Fields::DEFAULT_IFS)
19
+ end
20
+
21
+ def parameter(name)
22
+ return array_read(name).to_s if name.is_a?(Reference)
23
+ if @arrays.key?(name)
24
+ array = @arrays[name]
25
+ return array.values[array.associative ? "0" : 0].to_s
26
+ end
27
+ case name
28
+ when "?" then @status.to_s
29
+ when "!" then @last_background_pid.to_s
30
+ when "$" then @shell_pid.to_s
31
+ when "-" then Builtins::SHELL_OPTIONS.select { |_, name| @options.include?(name) }.keys.join + (@interactive ? "i" : "")
32
+ when "#" then @arguments.length.to_s
33
+ when "@" then @arguments.join(" ")
34
+ when "*" then @arguments.join(ifs[0].to_s)
35
+ when "0" then @name
36
+ when /\A\d+\z/ then @arguments[name.to_i - 1].to_s
37
+ else @variables.fetch(name, "")
38
+ end
39
+ end
40
+
41
+ def parameter_set?(name)
42
+ if name.is_a?(Reference)
43
+ return !array_values(name).empty? if %w[@ *].include?(name.subscript.literal)
44
+ return !array_read(name).nil?
45
+ end
46
+ if @arrays.key?(name)
47
+ array = @arrays[name]
48
+ return array.values.key?(array.associative ? "0" : 0)
49
+ end
50
+ case name
51
+ when "@", "*" then !@arguments.empty?
52
+ when "!" then !@last_background_pid.nil?
53
+ when "?", "$", "#", "0", "-" then true
54
+ when /\A\d+\z/ then name.to_i <= @arguments.length
55
+ else @variables.key?(name)
56
+ end
57
+ end
58
+
59
+ def arithmetic_value(expression)
60
+ if expression.is_a?(Word)
61
+ expression = expression.parts.map do |part|
62
+ part.kind == :literal ? part.text : scalar(Word.new(parts: [part]))
63
+ end.join
64
+ end
65
+ Arithmetic.evaluate(expression, assign: method(:assign)) do |name|
66
+ raise ExpansionError, "#{name}: unbound variable" if @options.include?("nounset") && !parameter_set?(name)
67
+ parameter(name)
68
+ end.to_s
69
+ rescue Arithmetic::Error => e
70
+ raise ExecutionError, e.message
71
+ end
72
+
73
+ def expand(word, assignment: false)
74
+ words = assignment ? [word] : Braces.expand(word)
75
+ words.flat_map { |item| expand_word(item, assignment: assignment) }
76
+ rescue Pattern::LimitError => e
77
+ raise ExpansionError, e.message
78
+ end
79
+
80
+ def expand_word(word, assignment: false)
81
+ groups = word_fields(word, assignment: assignment)
82
+ return [groups.map { |group| Fields.value(group) }.join(" ")] if assignment
83
+
84
+ groups.flat_map do |group|
85
+ preserve_empty = group.any? { |character| character.quoted && character.text.empty? }
86
+ group = group.reject { |character| character.text.empty? }
87
+ ranges = Fields.ranges(group, ifs)
88
+ next [""] if ranges.empty? && preserve_empty
89
+ ranges.flat_map do |range|
90
+ characters = group[range]
91
+ value = Fields.value(characters)
92
+ next [value] if @options.include?("noglob")
93
+ pattern = Fields.pattern(characters, glob: true)
94
+ matches = if pattern.match?(/[*?\[]/)
95
+ Locale.with(@variables) { |locale| Glob.new(pattern, cwd: @directory, locale: locale, globstar: @globstar).matches }
96
+ else
97
+ []
98
+ end
99
+ matches.empty? ? [value] : matches
100
+ end
101
+ end
102
+ end
103
+
104
+ def scalar(word)
105
+ word_fields(word).map { |group| Fields.value(group) }.join(" ")
106
+ end
107
+
108
+ def pattern(word)
109
+ word_fields(word).map { |group| Fields.pattern(group) }.join(" ")
110
+ end
111
+
112
+ def matches_pattern?(pattern, value)
113
+ Locale.with(@variables) { |locale| Pattern.new(pattern, locale: locale).match?(value) }
114
+ rescue Pattern::LimitError => e
115
+ raise ExpansionError, e.message
116
+ end
117
+
118
+ def word_fields(word, expanded: false, assignment: false)
119
+ groups = [[]]
120
+ word.parts.each_with_index do |part, index|
121
+ additions = case part.kind
122
+ when :parameter
123
+ parameter_fields(part.text, part.quoted)
124
+ when :command
125
+ [Fields.characters(capture_substitution(part.text), quoted: part.quoted, split: !part.quoted)]
126
+ when :process
127
+ [Fields.characters(process_substitution(*part.text), quoted: true)]
128
+ when :arithmetic
129
+ [Fields.characters(arithmetic_value(part.text), quoted: part.quoted, split: !part.quoted)]
130
+ else
131
+ [literal_fields(part, first: index.zero?, following: word.parts[index + 1], assignment: assignment, expanded: expanded)]
132
+ end
133
+ next if additions.empty?
134
+ groups.last.concat(additions.first)
135
+ groups.concat(additions.drop(1))
136
+ end
137
+ groups
138
+ rescue ExecutionError, Pattern::LimitError => e
139
+ raise ExpansionError, e.message
140
+ end
141
+
142
+ def literal_fields(part, first:, following:, assignment:, expanded:)
143
+ return Fields.characters(part.text, quoted: true) if part.quoted
144
+ pieces = assignment ? part.text.split(":", -1) : [part.text]
145
+ pieces.each_with_index.flat_map do |piece, index|
146
+ prefix = assignment && first && index.zero? ? piece[/\A[A-Za-z_][A-Za-z0-9_]*=/].to_s : ""
147
+ value = piece.delete_prefix(prefix)
148
+ match = value.match(/\A~([^\/]*)(\/.*)?\z/m) if first || index.positive?
149
+ continued = index == pieces.length - 1 && following && match && !match[2]
150
+ home = tilde_home(match[1]) if match && !continued
151
+ fields = if home
152
+ Fields.characters(prefix, split: expanded) + Fields.characters(home, quoted: true) + Fields.characters(match[2].to_s, split: expanded)
153
+ else
154
+ Fields.characters(piece, split: expanded)
155
+ end
156
+ index.zero? ? fields : Fields.characters(":", split: expanded) + fields
157
+ end
158
+ end
159
+
160
+ def tilde_home(name)
161
+ case name
162
+ when "" then @variables.fetch("HOME") { Etc.getpwuid.dir }
163
+ when "+" then @variables["PWD"]
164
+ when "-" then @variables["OLDPWD"]
165
+ else Etc.getpwnam(name).dir
166
+ end
167
+ rescue ArgumentError
168
+ nil
169
+ end
170
+
171
+ def parameter_fields(expression, quoted)
172
+ return parameter_value_fields(expression, quoted) unless expression.is_a?(Parameter)
173
+
174
+ name, operator, operand = expression.name, expression.operator, expression.word
175
+ value = parameter(name)
176
+ if operator == :keys
177
+ return array_fields(name, quoted, keys: true)
178
+ end
179
+ if operator == :length
180
+ all = name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
181
+ raise ExpansionError, "#{name}: unbound variable" if @options.include?("nounset") && !all && !%w[@ *].include?(name) && !parameter_set?(name)
182
+ length = if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
183
+ array_values(name).length
184
+ else
185
+ %w[@ *].include?(name) ? @arguments.length : Locale.with(@variables) { |locale| locale.text(value).length }
186
+ end
187
+ return [Fields.characters(length.to_s, quoted: quoted, split: !quoted)]
188
+ end
189
+ if operator.start_with?("#", "%")
190
+ source = pattern(operand)
191
+ values = if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
192
+ array_values(name)
193
+ else
194
+ %w[@ *].include?(name) ? @arguments : [value]
195
+ end
196
+ values = Locale.with(@variables) do |locale|
197
+ match = Pattern.new(source, locale: locale)
198
+ values.map { |item| remove_pattern(locale.text(item), match, operator) }
199
+ end
200
+ return parameter_value_fields(name, quoted, values: values)
201
+ end
202
+
203
+ empty = value.empty? && !(quoted && %w[@ *].include?(name))
204
+ missing = !parameter_set?(name) || operator.start_with?(":") && empty
205
+ action = operator.delete_prefix(":")
206
+ use_operand = action == "+" ? !missing : missing
207
+ return action == "+" ? [Fields.characters("", quoted: quoted)] : parameter_value_fields(name, quoted) unless use_operand
208
+
209
+ groups = word_fields(operand, expanded: true)
210
+ groups = [groups.flatten] if ifs.empty?
211
+ result = groups.map { |group| Fields.value(group) }.join(ifs[0].to_s)
212
+ case action
213
+ when "="
214
+ raise ExpansionError, "#{name}: cannot assign a special or positional parameter" unless name.is_a?(Reference) || name.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)
215
+ assign(name, result)
216
+ when "?"
217
+ message = operand.parts.empty? ? "parameter is unset#{operator.start_with?(":") ? ' or null' : ''}" : result
218
+ raise ExpansionError, "#{name}: #{message}"
219
+ end
220
+ groups.first.concat(Fields.characters("", quoted: true)) if quoted && groups.all?(&:empty?)
221
+ groups
222
+ end
223
+
224
+ def parameter_value_fields(name, quoted, values: nil)
225
+ all = name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
226
+ if @options.include?("nounset") && !all && !%w[@ *].include?(name) && !parameter_set?(name)
227
+ raise ExpansionError, "#{name}: unbound variable"
228
+ end
229
+ if name.is_a?(Reference) && %w[@ *].include?(name.subscript.literal)
230
+ return array_fields(name, quoted, values: values)
231
+ end
232
+ if name == "@" || name == "*" && !quoted
233
+ (values || @arguments).map { |value| Fields.characters(value, quoted: quoted, split: !quoted) }
234
+ else
235
+ value = values ? values.join(name == "*" ? ifs[0].to_s : " ") : parameter(name)
236
+ [Fields.characters(value, quoted: quoted, split: !quoted)]
237
+ end
238
+ end
239
+
240
+ def remove_pattern(value, pattern, operator)
241
+ sizes = operator.length == 2 ? value.length.downto(0) : 0.upto(value.length)
242
+ prefix = operator.start_with?("#")
243
+ sizes.each do |size|
244
+ candidate = prefix ? value[0, size] : value[value.length - size, size]
245
+ next unless pattern.match?(candidate)
246
+ return prefix ? value[size..] : value[0, value.length - size]
247
+ end
248
+ value
249
+ end
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,62 @@
1
+ module Kward
2
+ module NativeShell
3
+ # Shared field splitting for word expansion and read. Quote provenance stays
4
+ # attached to characters; literal text is never split merely because it is IFS.
5
+ module Fields
6
+ Character = Struct.new(:text, :quoted, :split, keyword_init: true)
7
+ DEFAULT_IFS = " \t\n".freeze
8
+
9
+ def self.characters(value, quoted: false, split: false)
10
+ result = value.each_char.map { |text| Character.new(text: text, quoted: quoted, split: split) }
11
+ result << Character.new(text: "", quoted: true, split: false) if value.empty? && quoted
12
+ result
13
+ end
14
+
15
+ def self.separator?(character, ifs)
16
+ character && character.split && !character.text.empty? && ifs.include?(character.text)
17
+ end
18
+
19
+ def self.whitespace?(character, ifs)
20
+ separator?(character, ifs) && DEFAULT_IFS.include?(character.text)
21
+ end
22
+
23
+ # Ranges retain original separators for read's final, unsplit variable.
24
+ def self.ranges(characters, ifs, limit: nil)
25
+ result = []
26
+ index = 0
27
+ index += 1 while whitespace?(characters[index], ifs)
28
+ while index < characters.length
29
+ start = index
30
+ if limit && result.length >= limit
31
+ finish = characters.length
32
+ finish -= 1 while finish > start && whitespace?(characters[finish - 1], ifs)
33
+ result << (start...finish) if finish > start
34
+ break
35
+ end
36
+ index += 1 while index < characters.length && !separator?(characters[index], ifs)
37
+ result << (start...index)
38
+ if whitespace?(characters[index], ifs)
39
+ index += 1 while whitespace?(characters[index], ifs)
40
+ index += 1 if separator?(characters[index], ifs)
41
+ elsif separator?(characters[index], ifs)
42
+ index += 1
43
+ end
44
+ index += 1 while whitespace?(characters[index], ifs)
45
+ end
46
+ result
47
+ end
48
+
49
+ def self.value(characters)
50
+ characters.map(&:text).join
51
+ end
52
+
53
+ def self.pattern(characters, glob: false)
54
+ characters.map do |character|
55
+ next character.text unless character.quoted || glob
56
+ special = character.quoted ? /[\\*?\[\]{}!^\-]/ : /[{}]/
57
+ character.text.gsub(special) { |text| "\\#{text}" }
58
+ end.join
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,96 @@
1
+ module Kward
2
+ module NativeShell
3
+ # Path traversal uses the same bounded pattern matcher as case and trimming.
4
+ # Recursive ** never follows symlinked directories or visits hidden entries.
5
+ class Glob
6
+ MAX_ENTRIES = 100_000
7
+ MAX_DEPTH = 100
8
+
9
+ def initialize(pattern, cwd:, locale:, globstar: false)
10
+ @parts = pattern.split("/", -1)
11
+ @absolute = pattern.start_with?("/")
12
+ @parts.shift if @absolute
13
+ @root = @absolute ? "/" : cwd
14
+ @locale = locale
15
+ @globstar = globstar
16
+ @matches = []
17
+ @visited = 0
18
+ end
19
+
20
+ def matches
21
+ root = @root
22
+ prefix = +(@absolute ? "/" : "")
23
+ index = 0
24
+ if root.respond_to?(:access_path)
25
+ # Resolve literal directory prefixes through the retained descriptor,
26
+ # including ../patterns when the current directory has been removed.
27
+ while index < @parts.length - 1 && !Pattern.new(@parts[index], locale: @locale).wildcards?
28
+ prefix << @parts[index].gsub(/\\(.)/m, '\1') << "/"
29
+ index += 1
30
+ end
31
+ opened = Directory.new(prefix, base: root) unless prefix.empty?
32
+ root = (opened || root).access_path
33
+ end
34
+ walk(root, prefix, index, index)
35
+ @matches.uniq.sort { |left, right| @locale.compare(left, right) }
36
+ rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EACCES
37
+ []
38
+ ensure
39
+ opened&.close
40
+ end
41
+
42
+ private
43
+
44
+ def entries(directory)
45
+ Dir.each_child(directory).each_with_object([]) do |name, result|
46
+ @visited += 1
47
+ raise ExpansionError, "pathname expansion exceeds #{MAX_ENTRIES} entries" if @visited > MAX_ENTRIES
48
+ result << name
49
+ end
50
+ rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EACCES
51
+ []
52
+ end
53
+
54
+ def walk(directory, prefix, index, depth, expanding: false)
55
+ raise ExpansionError, "pathname expansion exceeds #{MAX_DEPTH} directory levels" if depth > MAX_DEPTH
56
+ part = @parts[index]
57
+ if part == ""
58
+ if index == @parts.length - 1
59
+ @matches << prefix unless prefix.empty?
60
+ else
61
+ walk(directory, prefix + "/", index + 1, depth)
62
+ end
63
+ return
64
+ end
65
+ recursive = @globstar && part == "**"
66
+ if recursive
67
+ if index == @parts.length - 1
68
+ @matches << prefix unless prefix.empty? || expanding
69
+ else
70
+ walk(directory, prefix, index + 1, depth)
71
+ end
72
+ end
73
+ matcher = Pattern.new(part, locale: @locale)
74
+ names = matcher.wildcards? ? entries(directory) : [part.gsub(/\\(.)/m, '\1')]
75
+ names.each do |name|
76
+ next if name.start_with?(".") && !part.start_with?(".", "\\.")
77
+ next unless matcher.match?(name)
78
+ path = File.join(directory, name)
79
+ next unless File.exist?(path) || File.symlink?(path)
80
+ if index == @parts.length - 1
81
+ @matches << prefix + name
82
+ elsif File.directory?(path)
83
+ walk(path, prefix + name + "/", index + 1, depth + 1) unless recursive
84
+ end
85
+ if recursive && File.directory?(path)
86
+ if File.symlink?(path)
87
+ @matches << prefix + name + "/" if @parts[(index + 1)..] == [""]
88
+ else
89
+ walk(path, prefix + name + "/", index, depth + 1, expanding: true)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end