From d26a40887032985c94edbf1ad96b2f0f047afe81 Mon Sep 17 00:00:00 2001 From: Cthulhux Date: Wed, 29 Nov 2023 02:22:49 +0100 Subject: [PATCH 1/2] Support the rc shell. Fixes #16264 (In my tests, changes to `shellenv.sh` weren't required, so I didn't bother.) --- Library/Homebrew/utils/shell.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index 6a903d4c88..bc7257c993 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -13,7 +13,7 @@ module Utils shell_name = File.basename(path) # handle possible version suffix like `zsh-5.2` shell_name.sub!(/-.*\z/m, "") - shell_name.to_sym if %w[bash csh fish ksh mksh sh tcsh zsh].include?(shell_name) + shell_name.to_sym if %w[bash csh fish ksh mksh rc sh tcsh zsh].include?(shell_name) end sig { params(default: String).returns(String) } @@ -42,6 +42,8 @@ module Utils # a single quote can be included in a single-quoted string via \' # and a literal \ can be included via \\ "set -gx #{key} \"#{sh_quote(value)}\"" + when :rc + "#{key}=(#{rc_quote(value)})" when :csh, :tcsh "setenv #{key} #{csh_quote(value)};" end @@ -54,6 +56,9 @@ module Utils when :bash bash_profile = "#{Dir.home}/.bash_profile" return bash_profile if File.exist? bash_profile + when :rc + rc_profile = "#{Dir.home}/.rcrc" + return rc_profile if File.exist? rc_profile when :zsh return "#{ENV["ZDOTDIR"]}/.zshrc" if ENV["ZDOTDIR"].present? end @@ -66,6 +71,8 @@ module Utils case preferred when :bash, :ksh, :sh, :zsh, nil "echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}" + when :rc + "echo '#{variable}=(#{sh_quote(value)})' >> #{profile}" when :csh, :tcsh "echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}" when :fish @@ -78,6 +85,8 @@ module Utils case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}" + when :rc + "echo 'path=(#{rc_quote(path)} $path)' >> #{profile}" when :csh, :tcsh "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}" when :fish @@ -91,6 +100,7 @@ module Utils fish: "~/.config/fish/config.fish", ksh: "~/.kshrc", mksh: "~/.kshrc", + rc: "~/.rcrc", sh: "~/.profile", tcsh: "~/.tcshrc", zsh: "~/.zshrc", @@ -124,5 +134,19 @@ module Utils str.gsub!(/\n/, "'\n'") str end + + sig { params(str: String).returns(String) } + def rc_quote(str) + # ruby's implementation of shell_escape + str = str.to_s + return "''" if str.empty? + + str = str.dup + # anything that isn't a known safe character is padded + str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation + str.gsub!(/'/, "''") + str.gsub!(/\n/, "'\n'") + str + end end end From 1c4c3194e65440808e015ecea281539efb17e1b5 Mon Sep 17 00:00:00 2001 From: Cthulhux Date: Wed, 29 Nov 2023 21:40:51 +0100 Subject: [PATCH 2/2] got rid of rc_quote The only "special" case about `rc` quoting is that strings that contain apostrophes need to contain double apostrophes instead. It seems unlikely that this will happen in Homebrew. --- Library/Homebrew/utils/shell.rb | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index bc7257c993..1f5e3844f9 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -43,7 +43,7 @@ module Utils # and a literal \ can be included via \\ "set -gx #{key} \"#{sh_quote(value)}\"" when :rc - "#{key}=(#{rc_quote(value)})" + "#{key}=(#{sh_quote(value)})" when :csh, :tcsh "setenv #{key} #{csh_quote(value)};" end @@ -86,7 +86,7 @@ module Utils when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}" when :rc - "echo 'path=(#{rc_quote(path)} $path)' >> #{profile}" + "echo 'path=(#{sh_quote(path)} $path)' >> #{profile}" when :csh, :tcsh "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}" when :fish @@ -134,19 +134,5 @@ module Utils str.gsub!(/\n/, "'\n'") str end - - sig { params(str: String).returns(String) } - def rc_quote(str) - # ruby's implementation of shell_escape - str = str.to_s - return "''" if str.empty? - - str = str.dup - # anything that isn't a known safe character is padded - str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation - str.gsub!(/'/, "''") - str.gsub!(/\n/, "'\n'") - str - end end end