Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

BCAT_COMMAND is ignored #9

Closed
plambert opened this issue Sep 10, 2010 · 3 comments
Closed

BCAT_COMMAND is ignored #9

plambert opened this issue Sep 10, 2010 · 3 comments

Comments

@plambert
Copy link

% echo foo | env BCAT_COMMAND="echo >/dev/tty" bcat
[shows foo in my default browser as usual]

From browser.rb, it seems like the BCAT_COMMAND value is stored into @command, but @command isn't used.

Commenting out the "command = browser_command" so that the #{command} will call self.command works.

I've a patch below for this problem and the quoting problem (which I'll open a bug for after this):

--- /opt/local/lib/ruby/gems/1.8/gems/bcat-0.5.1/lib/bcat/browser.rb.original   2010-09-10 10:47:06.000000000 -0700
+++ /opt/local/lib/ruby/gems/1.8/gems/bcat-0.5.1/lib/bcat/browser.rb    2010-09-10 11:31:02.000000000 -0700
@@ -37,33 +37,39 @@
       'gnome'         => 'epiphany'
     }

-    def initialize(browser, command=ENV['BCAT_COMMAND'])
+    def initialize(browser, command=nil)
       @browser = browser
-      @command = command
+      @command = command || ENV['BCAT_COMMAND'] || command_for_browser(@browser)
     end

+    # open a given url
     def open(url)
-      command = browser_command
-      fork do
-        [$stdin, $stdout].each { |fd| fd.close }
-        exec "#{command} '#{shell_quote(url)}'"
-      end
-    end
-
-    def command
-      return @command if @command
-      browser_command
+      # run the browser command with stdin, stdout, and stderr closed
+      system "#{@command} </dev/null >/dev/null 2>&1 #{shell_quote(url)}"
     end

-    def browser_command(browser=@browser)
-      browser ||= 'default'
-      browser = browser.downcase
-      browser = ALIASES[browser] || browser
-      COMMANDS[ENVIRONMENT][browser]
+    # return the command to run for a given browser
+    def command_for_browser(browser='default')
+      browser = ALIASES[browser] || browser.downcase
+      COMMANDS[ENVIRONMENT][browser] || COMMANDS[ENVIRONMENT]['default']
     end

+    # return a given string quoted for a POSIX shell
     def shell_quote(argument)
-      arg = argument.to_s.gsub(/([\\'])/) { "\\" + $1 }
+      argument=argument.to_s
+      # if the argument is a simple, single word, leave it alone
+      if argument =~ /^[a-zA-Z0-9_+@%=,.\/\-]+$/
+        argument
+      else
+        # replace any backslashes with escaped versions
+        argument.gsub!(/(\\)/, "\\\\")
+        # single quotes cannot be quoted inside single quotes, so we
+        # end the single quoted string, put a double-quoted string containing a
+        # single quote, and then start a new single-quoted string.  ugly, but works
+        argument.gsub!(/'/, %w{'"'"'})
+        # and wrap it in single quotes
+        "'#{argument}'"
+      end
     end
   end
 end
@rtomayko
Copy link
Owner

Got it. Thanks. In the future, could you submit these via pull request or use git format-patch so that I can attribute you properly?

@rtomayko
Copy link
Owner

fix shell quoting single quotes

closed by f61c6ee

@plambert
Copy link
Author

Thanks! Now that I've learned a lot more about git, I will definitely submit pull requests in the future!

bps pushed a commit that referenced this issue Apr 18, 2011
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants