From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: sbaugh@catern.com Newsgroups: gmane.emacs.bugs Subject: bug#65902: 29.0.92; emacsclient-mail.desktop fails due to complicated escaping Date: Thu, 14 Sep 2023 11:18:13 +0000 (UTC) Message-ID: <87edj1t34b.fsf@catern.com> References: <83ttrym8jx.fsf@gnu.org> <83led9nay9.fsf@gnu.org> <66a6c09e-3b61-d913-5638-4c804fb826f6@gmail.com> <83edj1mja5.fsf@gnu.org> <87il8dt3sh.fsf@catern.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37163"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 65902@debbugs.gnu.org, Jim Porter , sbaugh@janestreet.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Sep 14 13:19:15 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qgkNO-0009RQ-DN for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 14 Sep 2023 13:19:15 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qgkN8-0000jU-RI; Thu, 14 Sep 2023 07:18:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qgkN6-0000ip-ES for bug-gnu-emacs@gnu.org; Thu, 14 Sep 2023 07:18:56 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qgkN6-0000o2-61 for bug-gnu-emacs@gnu.org; Thu, 14 Sep 2023 07:18:56 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qgkNB-0005Zn-Lr for bug-gnu-emacs@gnu.org; Thu, 14 Sep 2023 07:19:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: sbaugh@catern.com Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 14 Sep 2023 11:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65902 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 65902-submit@debbugs.gnu.org id=B65902.169469031021397 (code B ref 65902); Thu, 14 Sep 2023 11:19:01 +0000 Original-Received: (at 65902) by debbugs.gnu.org; 14 Sep 2023 11:18:30 +0000 Original-Received: from localhost ([127.0.0.1]:37828 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qgkMf-0005Z3-DD for submit@debbugs.gnu.org; Thu, 14 Sep 2023 07:18:30 -0400 Original-Received: from s.wrqvtzvf.outbound-mail.sendgrid.net ([149.72.126.143]:22236) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qgkMa-0005Ym-EY for 65902@debbugs.gnu.org; Thu, 14 Sep 2023 07:18:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=catern.com; h=from:subject:in-reply-to:references:mime-version:to:cc:content-type: cc:content-type:from:subject:to; s=s1; bh=VW/UV2g9DzSt763rbHdI0QwrZHDDIGVyGo6eN5I+p8E=; b=ngOvkqOiTriBjnq2scW3Br2d4AqnEBO6jH3CtxvSYv8KkAY2D5wmir7MOb/fTH77Y7XD xbHk+8J7wTezEM9XnYzHVojVPgdv8ZuTD286xtLJxUCL63Fd1ugj7yoFitG8pbatzHNoTb 6nCX1lnhGfTn1yTNzZl43ZeAtkcqOWEbgQNB7SW5u6xQjKELMZXy6ux4smV41PRMiyvYy7 GyRJAXxq3BhPVUKLiqKRyuF7tpkCJdz6ltY9VWukQcAFysdFwnKzNvhu6y55nCTr+mC4Cs OVvktwfUlvVRfgQyTU6hkvgV0jaehcHxNUGNSCsc9eeB4QLGTI8wApcLg+FTmM8A== Original-Received: by filterdrecv-d7bbbc8bf-b8xkx with SMTP id filterdrecv-d7bbbc8bf-b8xkx-1-6502EBF4-1D 2023-09-14 11:18:12.954633677 +0000 UTC m=+10928306.467982787 Original-Received: from earth.catern.com (unknown) by geopod-ismtpd-8 (SG) with ESMTP id KZBwylntSfOfKyy8ktfmBw Thu, 14 Sep 2023 11:18:12.807 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=::1; helo=localhost; envelope-from=sbaugh@catern.com; receiver=gnu.org Original-Received: from localhost (localhost [IPv6:::1]) by earth.catern.com (Postfix) with ESMTPSA id 3C76B60129; Thu, 14 Sep 2023 07:18:12 -0400 (EDT) In-Reply-To: <87il8dt3sh.fsf@catern.com> (sbaugh@catern.com's message of "Thu, 14 Sep 2023 11:03:44 +0000 (UTC)") X-SG-EID: ZgbRq7gjGrt0q/Pjvxk7wM0yQFRdOkTJAtEbkjCkHbJZGaI4O8qNQ4TgjNkgKQBtPLyhyhmLAJUHmm8ZUshkasbKFJ2tGwjGJVwODdehFfuxzxFk9gkOQc08uLBFVT44sp5cn+5PFiS37btt9H+JJ4QiFBVcRy7elBUJoOyV2y6IqAVZ96Qa+3SXhgamLqi9kWsGxtrz82abX9kuvGamKg== X-Entity-ID: d/0VcHixlS0t7iB1YKCv4Q== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:270412 Archived-At: --=-=-= Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Here's a revised patch which drops adding the argument to Emacs itself; it only adds an argument to emacsclient. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-apply-argument-to-emacsclient-to-avoid-escaping.patch >From d4558462598eaa33466b3b2fb1dbcc26ea877ede Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Thu, 14 Sep 2023 07:12:22 -0400 Subject: [PATCH] Add --apply argument to emacsclient to avoid escaping Passing arguments to functions through emacsclient --eval requires complicated escaping (as seen in emacsclient-mail.desktop before this change). The new --apply argument for emacsclient passes command line arguments as uninterpreted strings to the specified function. This simplifies use cases where arbitrary input needs to be passed to Emacs. org-protocol will be able to use this as well, which will allow it to eventually drop its current advice on server-visit-files. * etc/emacsclient-mail.desktop: Use --apply. (bug#65902) * lib-src/emacsclient.c (longopts, decode_options, main): Add support for --apply. * lisp/server.el (server-apply-and-print): Add. (server-process-filter): Add support for -apply and -applyargs --- etc/emacsclient-mail.desktop | 7 ++----- lib-src/emacsclient.c | 36 ++++++++++++++++++++++++++++++++++-- lisp/server.el | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/etc/emacsclient-mail.desktop b/etc/emacsclient-mail.desktop index 0a2420ddead..ea0690bacd9 100644 --- a/etc/emacsclient-mail.desktop +++ b/etc/emacsclient-mail.desktop @@ -1,10 +1,7 @@ [Desktop Entry] Categories=Network;Email; Comment=GNU Emacs is an extensible, customizable text editor - and more -# We want to pass the following commands to the shell wrapper: -# u=$(echo "$1" | sed 's/[\"]/\\&/g'); exec emacsclient --alternate-editor= --display="$DISPLAY" --eval "(message-mailto \"$u\")" -# Special chars '"', '$', and '\' must be escaped as '\\"', '\\$', and '\\\\'. -Exec=sh -c "u=\\$(echo \\"\\$1\\" | sed 's/[\\\\\\"]/\\\\\\\\&/g'); exec emacsclient --alternate-editor= --display=\\"\\$DISPLAY\\" --eval \\"(message-mailto \\\\\\"\\$u\\\\\\")\\"" sh %u +Exec=emacsclient --alternate-editor= --apply message-mailto -- %u Icon=emacs Name=Emacs (Mail, Client) MimeType=x-scheme-handler/mailto; @@ -16,7 +13,7 @@ Actions=new-window;new-instance; [Desktop Action new-window] Name=New Window -Exec=sh -c "u=\\$(echo \\"\\$1\\" | sed 's/[\\\\\\"]/\\\\\\\\&/g'); exec emacsclient --alternate-editor= --create-frame --eval \\"(message-mailto \\\\\\"\\$u\\\\\\")\\"" sh %u +Exec=emacsclient --alternate-editor= --create-frame --apply message-mailto -- %u [Desktop Action new-instance] Name=New Instance diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c index 698bf9b50ae..159c22d1ae9 100644 --- a/lib-src/emacsclient.c +++ b/lib-src/emacsclient.c @@ -116,6 +116,9 @@ #define DEFAULT_TIMEOUT (30) /* True means args are expressions to be evaluated. --eval. */ static bool eval; +/* The function to call. Other arguments are passed as strings. --apply. */ +static char *apply; + /* True means open a new frame. --create-frame etc. */ static bool create_frame; @@ -169,6 +172,7 @@ #define DEFAULT_TIMEOUT (30) { "quiet", no_argument, NULL, 'q' }, { "suppress-output", no_argument, NULL, 'u' }, { "eval", no_argument, NULL, 'e' }, + { "apply", required_argument, NULL, 'y' }, { "help", no_argument, NULL, 'H' }, { "version", no_argument, NULL, 'V' }, { "tty", no_argument, NULL, 't' }, @@ -552,6 +556,10 @@ decode_options (int argc, char **argv) eval = true; break; + case 'y': + apply = optarg; + break; + case 'q': quiet = true; break; @@ -690,6 +698,7 @@ print_help_and_exit (void) -F ALIST, --frame-parameters=ALIST\n\ Set the parameters of a new frame\n\ -e, --eval Evaluate the FILE arguments as ELisp expressions\n\ +-y, --apply FUNC Call ELisp FUNC, passing all FILE arguments as strings\n\ -n, --no-wait Don't wait for the server to return\n\ -w, --timeout=SECONDS Seconds to wait before timing out\n\ -q, --quiet Don't display messages on success\n\ @@ -1953,7 +1962,7 @@ main (int argc, char **argv) /* Process options. */ decode_options (argc, argv); - if (! (optind < argc || eval || create_frame)) + if (! (optind < argc || eval || apply || create_frame)) { message (true, ("%s: file name or argument required\n" "Try '%s --help' for more information\n"), @@ -1961,6 +1970,14 @@ main (int argc, char **argv) exit (EXIT_FAILURE); } + if (eval && apply) + { + message (true, ("%s: can't pass both --eval and --apply\n" + "Try '%s --help' for more information\n"), + progname, progname); + exit (EXIT_FAILURE); + } + #ifdef SOCKETS_IN_FILE_SYSTEM if (tty) { @@ -2080,6 +2097,13 @@ main (int argc, char **argv) send_to_emacs (emacs_socket, " "); continue; } + else if (apply) + { + send_to_emacs (emacs_socket, "-applyarg "); + quote_argument (emacs_socket, argv[i]); + send_to_emacs (emacs_socket, " "); + continue; + } char *p = argv[i]; if (*p == '+') @@ -2136,10 +2160,18 @@ main (int argc, char **argv) send_to_emacs (emacs_socket, " "); } + if (apply) + { + send_to_emacs (emacs_socket, "-apply "); + quote_argument (emacs_socket, apply); + send_to_emacs (emacs_socket, " "); + } + + send_to_emacs (emacs_socket, "\n"); /* Wait for an answer. */ - if (!eval && !tty && !nowait && !quiet && 0 <= process_grouping ()) + if (!eval && !apply && !tty && !nowait && !quiet && 0 <= process_grouping ()) { printf ("Waiting for Emacs..."); skiplf = false; diff --git a/lisp/server.el b/lisp/server.el index c3325e5a24c..5981e90625d 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -873,6 +873,17 @@ server-eval-and-print (point-min) (point-max)))) (server-reply-print (server-quote-arg text) proc))))))) +(defun server-apply-and-print (func args proc) + "Call FUNC on ARGS and send the result back to client PROC." + (let ((v (with-local-quit (eval (apply (intern func) args) t)))) + (when proc + (with-temp-buffer + (let ((standard-output (current-buffer))) + (pp v) + (let ((text (buffer-substring-no-properties + (point-min) (point-max)))) + (server-reply-print (server-quote-arg text) proc))))))) + (defconst server-msg-size 1024 "Maximum size of a message sent to a client.") @@ -1196,6 +1207,7 @@ server-process-filter tty-type ; string. files filepos + applyargs args-left) ;; Remove this line from STRING. (setq string (substring string (match-end 0))) @@ -1323,6 +1335,28 @@ server-process-filter commands) (setq filepos nil))) + ;; -apply FUNC: Call a function on arguments. + ("-apply" + (if use-current-frame + (setq use-current-frame 'always)) + (let ((func (pop args-left))) + (if coding-system + (setq func (decode-coding-string func coding-system))) + (push (lambda () (server-apply-and-print func applyargs proc)) + commands) + (setq applyargs nil) + (setq filepos nil))) + + ;; -applyarg ARG: Add an argument for later -apply. + ("-applyarg" + (if use-current-frame + (setq use-current-frame 'always)) + (let ((arg (pop args-left))) + (if coding-system + (setq arg (decode-coding-string arg coding-system))) + (push arg applyargs) + (setq filepos nil))) + ;; -env NAME=VALUE: An environment variable. ("-env" (let ((var (pop args-left))) -- 2.41.0 --=-=-=--