From 2550e5d736b3d113567f0e766ebc33aa911fb49a Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Mon, 24 Sep 2007 11:48:14 +0000 Subject: [PATCH] :! command, fixed statusbar height properly, system() now returns the stdout string --- NEWS | 2 + chrome/content/vimperator/commands.js | 23 ++++++++-- chrome/content/vimperator/default.css | 4 +- chrome/content/vimperator/vimperator.js | 58 ++++++++++++++++++++----- 4 files changed, 73 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 8b02572d..c4e747d1 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ 2007-xx-xx: * version 0.6 * THIS VERSION ONLY WORKS WITH FIREFOX 3.0 + * :b2 now allowed, no space required before the 2 anymore + * :! to run commands through system() (UNIX only) * separated search and Ex command history * merge the existing status bar with the standard FF status bar so that security information and extension buttons are included diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index 8a3c313a..0bbb491a 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -42,7 +42,7 @@ function Command(specs, action, extra_info) //{{{ for (var i = 0; i < specs.length; i++) { var match; - if (match = specs[i].match(/(\w+)\[(\w+)\]/)) + if (match = specs[i].match(/(\w+|!)\[(\w+)\]/)) { short_names.push(match[1]); long_names.push(match[1] + match[2]); @@ -114,7 +114,7 @@ Command.prototype.hasName = function(name) { if (this.specs[i] == name) // literal command name return true; - else if (this.specs[i].match(/^\w+\[\w+\]$/)) // abbreviation spec + else if (this.specs[i].match(/^(\w+|!)\[\w+\]$/)) // abbreviation spec if (matchAbbreviation(name, this.specs[i])) return true; } @@ -194,7 +194,7 @@ function Commands() //{{{ } // 0 - count, 1 - cmd, 2 - special, 3 - args, 4 - heredoc tag - var matches = string.match(/^:*(\d+)?([a-zA-Z]+)(!)?(?:\s+(.*?)\s*)?$/); + var matches = string.match(/^:*(\d+)?([a-zA-Z]+|!)(!)?(?:\s*(.*?)\s*)?$/); if (!matches) return [null, null, null, null, null]; matches.shift(); @@ -1557,6 +1557,23 @@ function Commands() //{{{ "Normally this command operates on the text zoom, if used with [!] it operates on full zoom." } )); + addDefaultCommand(new Command(["!", "run"], + function(args, special) + { + // TODO: if special, run the last command + var output = vimperator.system(args) + if (typeof output === "string") + vimperator.echo("
" + output + "
"); + else + vimperator.echoerr("Invalid system command: " + args); + }, + { + usage: "!{command}", + short_help: "Run a command", + help: "Runs {command} through system() and displays its output." + + "Input redirection (< foo) not done, do not run commands which require stdin or it will hang Firefox!" + } + )); //}}} } //}}} diff --git a/chrome/content/vimperator/default.css b/chrome/content/vimperator/default.css index ae566269..fe7f85ec 100644 --- a/chrome/content/vimperator/default.css +++ b/chrome/content/vimperator/default.css @@ -144,8 +144,10 @@ fieldset.paypal { color: HighlightText !important; } -statusbarpanel { +/* fixes the min-height: 22px from firefox */ +#status-bar, statusbarpanel { -moz-appearance: none !important; + min-height: 18px !important; border: none !important; } #vimperator-statusline { diff --git a/chrome/content/vimperator/vimperator.js b/chrome/content/vimperator/vimperator.js index 43fade17..7135e2bd 100644 --- a/chrome/content/vimperator/vimperator.js +++ b/chrome/content/vimperator/vimperator.js @@ -124,6 +124,30 @@ const vimperator = (function() //{{{ return null; } + // TODO: make secure + // TODO: test if it actually works on windows + function getTempFile() + { + var file = Components.classes["@mozilla.org/file/local;1"]. + createInstance(Components.interfaces.nsILocalFile); + if (navigator.platform == "Win32") + { + var dir = environment_service.get("TMP") || environment_service.get("TEMP") || "C:\\"; + file.initWithPath(dir + "\\vimperator.tmp"); + } + else + { + var dir = environment_service.get("TMP") || environment_service.get("TEMP") || "/tmp/"; + file.initWithPath(dir + "/vimperator.tmp"); + } + file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600); + + if (file.exists()) + return file; + else + return null; + } + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ @@ -506,22 +530,36 @@ const vimperator = (function() //{{{ // when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is fixed // is fixed, should use that instead of a tmpfile // TODO: make it usable on windows - // TODO: check for errors/insecurities - system: function (str) + // TODO: pass "input" as stdin + // TODO: add shell/shellcmdflag options to replace "sh" and "-c" + system: function (str, input) { - var file = Components.classes["@mozilla.org/file/local;1"]. - createInstance(Components.interfaces.nsILocalFile); - var dir = environment_service.get("TMP") || environment_service.get("TEMP") || "/tmp/"; - file.initWithPath(dir + "vimperator.tmp"); - file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600); - this.run("sh", ["-c", str + " > " + file.path], true); - var fd = vimperator.fopen(file, "<"); + var fileout = getTempFile(); + if (!fileout) + return ""; + + var filein = null; + var command = str + " > \"" + fileout.path.replace('"', '\\"') + "\""; + if (input) + { + filein = getTempFile(); + var fdin = vimperator.fopen(filein, ">"); + fdin.write(input); + fdin.close(); + command += " < \"" + filein.path.replace('"', '\\"') + "\""; + } + + this.run("sh", ["-c", command], true); + var fd = vimperator.fopen(fileout, "<"); if (!fd) return null; var s = fd.read(); fd.close(); - file.remove(false); + fileout.remove(false); + if (filein) + filein.remove(false); + return s; },