From 4d250095cba36b32ce3e5083d453525209eeee06 Mon Sep 17 00:00:00 2001 From: Bryan Bishop Date: Sat, 22 Dec 2012 18:26:15 -0600 Subject: fix external viewer using shlex This makes spawn_external_viewer not crash when $EDITOR or $PAGER have spaces or multiple arguments. In addition, spawn_external_viewer now chmods the file to read-only to remind users who use only an $EDITOR that this function does not read the file when the user returns. Also, some of the redundant exception case handling for editing has been consolidated. fixes #79 --- libmproxy/console/__init__.py | 26 +++++++++++++++++--------- libmproxy/console/flowview.py | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'libmproxy/console') diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 544fbd6e..26f955d0 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import mailcap, mimetypes, tempfile, os, subprocess, glob, time, shlex +import mailcap, mimetypes, tempfile, os, subprocess, glob, time, shlex, stat import os.path, sys, weakref import urwid from .. import controller, utils, flow @@ -504,7 +504,7 @@ class ConsoleMaster(flow.FlowMaster): os.write(fd, data) os.close(fd) c = os.environ.get("EDITOR") - #If no EDITOR is set, assume 'vi' + # if no EDITOR is set, assume 'vi' if not c: c = "vi" cmd = shlex.split(c) @@ -513,12 +513,10 @@ class ConsoleMaster(flow.FlowMaster): try: subprocess.call(cmd) except: - self.statusbar.message("Can't start editor: %s" % c) - self.ui.start() - os.unlink(name) - return data + self.statusbar.message("Can't start editor: %s" % " ".join(c)) + else: + data = open(name).read() self.ui.start() - data = open(name).read() os.unlink(name) return data @@ -531,6 +529,9 @@ class ConsoleMaster(flow.FlowMaster): os.write(fd, data) os.close(fd) + # read-only to remind the user that this is a view function + os.chmod(name, stat.S_IREAD) + cmd = None shell = False @@ -540,10 +541,17 @@ class ConsoleMaster(flow.FlowMaster): if cmd: shell = True if not cmd: + # hm which one should get priority? c = os.environ.get("PAGER") or os.environ.get("EDITOR") - cmd = [c, name] + if not c: + c = "less" + cmd = shlex.split(c) + cmd.append(name) self.ui.stop() - subprocess.call(cmd, shell=shell) + try: + subprocess.call(cmd, shell=shell) + except: + self.statusbar.message("Can't start external viewer: %s" % " ".join(c)) self.ui.start() os.unlink(name) diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 3473c474..4215f170 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -343,7 +343,7 @@ class FlowView(common.WWrap): self.flow.backup() if part == "r": c = self.master.spawn_editor(conn.content or "") - conn.content = c.rstrip("\n") + conn.content = c.rstrip("\n") # what? elif part == "f": if not conn.get_form_urlencoded() and conn.content: self.master.prompt_onekey( -- cgit v1.2.3