unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Reuben Thomas <rrt@sc3d.org>
To: Glenn Morris <rgm@gnu.org>
Cc: 25082@debbugs.gnu.org
Subject: bug#25082: [PATCH] Add support to emacsclient for command-lline options in ALTERNATE_EDITOR/--alternate-editor
Date: Wed, 23 Aug 2017 23:59:01 +0100	[thread overview]
Message-ID: <ef44ddac-cef6-9422-5912-969aba2c1418@sc3d.org> (raw)
In-Reply-To: <g2oa0mvwyz.fsf@fencepost.gnu.org>

[-- Attachment #1: Type: text/plain, Size: 940 bytes --]

On 08/12/16 23:10, Glenn Morris wrote:
> Reuben Thomas wrote:
>
>> The attached patch adds support to emacsclient for command-line options
>> when specifying the alternate editor, so that for example one can now say:
>>
>> ALTERNATE_EDITOR="emacs -Q -nw"
> Obvious question: what happens if eg one wants to specify an absolute
> file name for the alternate editor, and it contains spaces?

I've modified my patch to cope with this case. Rather than write a
general escaping parser, or even one that just copes with escaping
quotes (likely to be complex and buggy in C; and I couldn't find code I
could easily lift from elsewhere), I simply allow " as a delimiter as
well as space for quotes. Escaping quotes is not supported.

This suffices to cope with the common case on Windows of needing to
quote a path containing spaces. It also copes with other simple cases.

Is this sufficient?

Updated patch attached.

-- 

https://rrt.sc3d.org


[-- Attachment #2: 0001-Add-support-for-arguments-in-ALTERNATE_EDITOR-to-ema.patch --]
[-- Type: text/x-patch, Size: 7346 bytes --]

From a9f38bcac3ee08f5542ebb4e775a23fff85aa6d5 Mon Sep 17 00:00:00 2001
From: Reuben Thomas <rrt@sc3d.org>
Date: Thu, 1 Dec 2016 15:21:57 +0000
Subject: [PATCH 1/2] Add support for arguments in ALTERNATE_EDITOR to
 emacsclient

* lib-src/emacsclient.c (fail): Parse ALTERNATE_EDITOR, or
corresponding command-line argument, into quote- or space-separated
tokens.  If a token starts with a quote, then it naturally is expected
to end with a quote; escaping is not supported. This is enough to cope
with the typical case of requiring the initial path to be quoted,
common on Windows where it may contain spaces.
* etc/NEWS: Document.
* test/lib-src/emacsclient-tests.el: Add a test.
---
 etc/NEWS                          |  4 ++
 lib-src/emacsclient.c             | 84 ++++++++++++++++++++++++++++++---------
 test/lib-src/emacsclient-tests.el | 50 +++++++++++++++++++++++
 3 files changed, 120 insertions(+), 18 deletions(-)
 create mode 100644 test/lib-src/emacsclient-tests.el

diff --git a/etc/NEWS b/etc/NEWS
index a9e2f5a..a0e001f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -511,6 +511,10 @@ Linum mode and all similar packages are henceforth becoming obsolete.
 Users and developers are encouraged to switch to this new feature
 instead.
 
++++
+** emacsclient now accepts command-line options in ALTERNATE_EDITOR
+and --alternate-editor. For example, ALTERNATE_EDITOR="emacs -Q -nw".
+
 \f
 * Editing Changes in Emacs 26.1
 
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index f1d4e89..5e181cc 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -110,6 +110,9 @@ char *w32_getenv (const char *);
 /* Name used to invoke this program.  */
 const char *progname;
 
+/* The first argument to main.  */
+int main_argc;
+
 /* The second argument to main.  */
 char **main_argv;
 
@@ -201,6 +204,35 @@ xmalloc (size_t size)
   return result;
 }
 
+/* Like realloc but get fatal error if memory is exhausted.  */
+
+static void *
+xrealloc (void *ptr, size_t size)
+{
+  void *result = realloc (ptr, size);
+  if (result == NULL)
+    {
+      perror ("realloc");
+      exit (EXIT_FAILURE);
+    }
+  return result;
+}
+
+/* Like strdup but get a fatal error if memory is exhausted. */
+char *xstrdup (const char *);
+
+char *
+xstrdup (const char *s)
+{
+  char *result = strdup (s);
+  if (result == NULL)
+    {
+      perror ("strdup");
+      exit (EXIT_FAILURE);
+    }
+  return result;
+}
+
 /* From sysdep.c */
 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
 
@@ -264,21 +296,6 @@ get_current_dir_name (void)
 
 #ifdef WINDOWSNT
 
-/* Like strdup but get a fatal error if memory is exhausted. */
-char *xstrdup (const char *);
-
-char *
-xstrdup (const char *s)
-{
-  char *result = strdup (s);
-  if (result == NULL)
-    {
-      perror ("strdup");
-      exit (EXIT_FAILURE);
-    }
-  return result;
-}
-
 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
 
 char *w32_get_resource (HKEY, const char *, LPDWORD);
@@ -673,7 +690,7 @@ Report bugs with M-x report-emacs-bug.\n");
 }
 
 /* Try to run a different command, or --if no alternate editor is
-   defined-- exit with an errorcode.
+   defined-- exit with an error code.
    Uses argv, but gets it from the global variable main_argv.  */
 
 static _Noreturn void
@@ -681,9 +698,38 @@ fail (void)
 {
   if (alternate_editor)
     {
-      int i = optind - 1;
+      size_t extra_args_size = (main_argc - optind + 1) * sizeof (char *);
+      size_t new_argv_size = extra_args_size;
+      char **new_argv = NULL;
+      char *s = xstrdup (alternate_editor);
+      unsigned toks = 0;
+
+      /* Unpack alternate_editor's space-separated tokens into new_argv.  */
+      for (char *tok = s; tok != NULL && *tok != '\0';)
+        {
+          /* Allocate new token.  */
+          ++toks;
+          new_argv = xrealloc (new_argv, new_argv_size + toks * sizeof (char *));
+
+          /* Skip leading delimiters, and set separator, skipping any
+             opening quote.  */
+          size_t skip = strspn (tok, " \"");
+          tok += skip;
+          char sep = (skip > 0 && tok[-1] == '"') ? '"' : ' ';
+
+          /* Record start of token.  */
+          new_argv[toks - 1] = tok;
+
+          /* Find end of token and overwrite it with NUL.  */
+          tok = strchr (tok, sep);
+          if (tok != NULL)
+            *tok++ = '\0';
+        }
+
+      /* Append main_argv arguments to new_argv.  */
+      memcpy (&new_argv[toks], main_argv + optind, extra_args_size);
 
-      execvp (alternate_editor, main_argv + i);
+      execvp (*new_argv, new_argv);
       message (true, "%s: error executing alternate editor \"%s\"\n",
 	       progname, alternate_editor);
     }
@@ -696,6 +742,7 @@ fail (void)
 int
 main (int argc, char **argv)
 {
+  main_argc = argc;
   main_argv = argv;
   progname = argv[0];
   message (true, "%s: Sorry, the Emacs server is supported only\n"
@@ -1629,6 +1676,7 @@ main (int argc, char **argv)
   int start_daemon_if_needed;
   int exit_status = EXIT_SUCCESS;
 
+  main_argc = argc;
   main_argv = argv;
   progname = argv[0];
 
diff --git a/test/lib-src/emacsclient-tests.el b/test/lib-src/emacsclient-tests.el
new file mode 100644
index 0000000..ea757f6
--- /dev/null
+++ b/test/lib-src/emacsclient-tests.el
@@ -0,0 +1,50 @@
+;;; emacsclient-tests.el --- Test emacsclient
+
+;; Copyright (C) 2016 Free Software Foundation, Inc.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(require 'ert)
+
+(defconst emacsclient-test-emacs
+  (expand-file-name "emacsclient" (concat
+                                   (file-name-directory
+                                    (directory-file-name
+                                     (file-name-directory invocation-directory)))
+                                   "lib-src"))
+  "Path to emacsclient binary in build tree.")
+
+(ert-deftest emacsclient-test-alternate-editor-allows-arguments ()
+  (let (process-environment process-environment)
+    (setenv "ALTERNATE_EDITOR" (concat
+                                (expand-file-name invocation-name invocation-directory)
+                                " --batch"))
+    (should (= 0 (call-process emacsclient-test-emacs nil nil nil "foo")))))
+
+(ert-deftest emacsclient-test-alternate-editor-allows-quotes ()
+  (let (process-environment process-environment)
+    (setenv "ALTERNATE_EDITOR" (concat
+                                "\""
+                                (expand-file-name invocation-name invocation-directory)
+                                "\"" " --batch"))
+    (should (= 0 (call-process emacsclient-test-emacs nil nil nil "foo")))))
+
+(provide 'emacsclient-tests)
+;;; emacsclient-tests.el ends here
-- 
2.7.4


  parent reply	other threads:[~2017-08-23 22:59 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-01 15:31 bug#25082: [PATCH] Add support to emacsclient for command-lline options in ALTERNATE_EDITOR/--alternate-editor Reuben Thomas
2016-12-02 10:20 ` Eli Zaretskii
2016-12-02 15:31   ` Reuben Thomas
2016-12-08 23:10 ` Glenn Morris
2016-12-09 13:44   ` Reuben Thomas
2017-08-20 21:08     ` Reuben Thomas
2017-08-22  0:16       ` Glenn Morris
2017-08-22  0:23         ` Reuben Thomas
2017-08-22  0:52           ` Reuben Thomas
2017-08-23 22:59   ` Reuben Thomas [this message]
2017-08-27 14:55     ` Eli Zaretskii
2017-08-28 10:15       ` Reuben Thomas
2017-08-29 15:43         ` Eli Zaretskii
2017-08-29 15:49           ` Reuben Thomas
2017-08-29 16:49             ` Eli Zaretskii
2017-08-29 22:29               ` Reuben Thomas
2017-08-30 16:48                 ` Eli Zaretskii
2017-08-30 21:01                   ` Reuben Thomas
2017-08-30 21:02 ` bug#25082: Patch installed Reuben Thomas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ef44ddac-cef6-9422-5912-969aba2c1418@sc3d.org \
    --to=rrt@sc3d.org \
    --cc=25082@debbugs.gnu.org \
    --cc=rgm@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).