1 ;;; shell-quasiquote.el --- Turn s-expressions into shell command strings.
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
6 ;; Keywords: extensions, unix
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; "Shell quasiquote" -- turn s-expressions into POSIX shell command strings.
26 ;; Shells other than POSIX sh are not supported.
28 ;; Quoting is automatic and safe against injection.
30 ;; (let ((file1 "file one")
31 ;; (file2 "file two"))
32 ;; (shqq (cp -r ,file1 ,file2 "My Files")))
33 ;; => "cp -r 'file one' 'file two' 'My Files'"
35 ;; You can splice many arguments into place with ,@foo.
37 ;; (let ((files (list "file one" "file two")))
38 ;; (shqq (cp -r ,@files "My Files")))
39 ;; => "cp -r 'file one' 'file two' 'My Files'"
41 ;; Note that the quoting disables a variety of shell expansions like ~/foo,
42 ;; $ENV_VAR, and e.g. {x..y} in GNU Bash.
44 ;; You can use ,,foo to escape the quoting.
46 ;; (let ((files "file1 file2"))
47 ;; (shqq (cp -r ,,files "My Files")))
48 ;; => "cp -r file1 file2 'My Files'"
50 ;; And ,,@foo to splice and escape quoting.
52 ;; (let* ((arglist '("-x 'foo bar' -y baz"))
53 ;; (arglist (append arglist '("-z 'qux fux'"))))
54 ;; (shqq (command ,,@arglist)))
55 ;; => "command -x 'foo bar' -y baz -z 'qux fux'"
62 (defun shqq--atom-to-string (atom)
64 ((symbolp atom) (symbol-name atom))
66 ((numberp atom) (number-to-string atom))
67 (t (error "Bad shqq atom: %S" atom))))
69 (defun shqq--quote-atom (atom)
70 (shell-quote-argument (shqq--atom-to-string atom)))
72 (defmacro shqq (parts)
73 "First, PARTS is turned into a list of strings. For this,
74 every element of PARTS must be one of:
76 - a symbol, evaluating to its name,
78 - a string, evaluating to itself,
80 - a number, evaluating to its decimal representation,
82 - \",expr\", where EXPR must evaluate to an atom that will be
83 interpreted according to the previous rules,
85 - \",@list-expr\", where LIST-EXPR must evaluate to a list whose
86 elements will each be interpreted like the EXPR in an \",EXPR\"
87 form, and spliced into the list of strings,
89 - \",,expr\", where EXPR is interpreted like in \",expr\",
91 - or \",,@expr\", where EXPR is interpreted like in \",@expr\".
93 In the resulting list of strings, all elements except the ones
94 resulting from \",,expr\" and \",,@expr\" forms are quoted for
97 Finally, the resulting list of strings is concatenated with
103 ((atom part) (shqq--quote-atom part))
104 ;; We use the match-comma helpers because pcase can't match ,foo.
106 ;; ,,foo i.e. (, (, foo))
107 (`(,`\, (,`\, ,form)) form)
108 ;; ,,@foo i.e. (, (,@ foo))
109 (`(,`\, (,`\,@ ,form)) `(mapconcat #'identity ,form " "))
111 (`(,`\, ,form) `(shqq--quote-atom ,form))
113 (`,@,form `(mapconcat #'shqq--quote-atom ,form " "))
115 (error "Bad shqq part: %S" part))))))
117 `(mapconcat #'identity (list ,@parts) " ")))
119 (provide 'shell-quasiquote)
123 ;;; shell-quasiquote.el ends here