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
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; "Shell quasiquote" -- turn s-expressions into POSIX shell command strings.
25 ;; Shells other than POSIX sh are not supported.
27 ;; Quoting is automatic and safe against injection.
29 ;; (let ((file1 "file one")
30 ;; (file2 "file two"))
31 ;; (shqq (cp -r ,file1 ,file2 "My Files")))
32 ;; => "cp -r 'file one' 'file two' 'My Files'"
34 ;; You can splice many arguments into place with ,@foo.
36 ;; (let ((files (list "file one" "file two")))
37 ;; (shqq (cp -r ,@files "My Files")))
38 ;; => "cp -r 'file one' 'file two' 'My Files'"
40 ;; Note that the quoting disables a variety of shell expansions like ~/foo,
41 ;; $ENV_VAR, and e.g. {x..y} in GNU Bash.
43 ;; You can use ,,foo to escape the quoting.
45 ;; (let ((files "file1 file2"))
46 ;; (shqq (cp -r ,,files "My Files")))
47 ;; => "cp -r file1 file2 'My Files'"
49 ;; And ,,@foo to splice and escape quoting.
51 ;; (let* ((arglist '("-x 'foo bar' -y baz"))
52 ;; (arglist (append arglist '("-z 'qux fux'"))))
53 ;; (shqq (command ,,@arglist)))
54 ;; => "command -x 'foo bar' -y baz -z 'qux fux'"
61 ;;; We don't use `shell-quote-argument' because it doesn't provide any safety
62 ;;; guarantees, and this quotes shell keywords as well.
63 (defun shqq--quote-string (string)
64 (concat "'" (replace-regexp-in-string "'" "'\\\\''" string) "'"))
66 (defun shqq--atom-to-string (atom)
68 ((symbolp atom) (symbol-name atom))
70 ((numberp atom) (number-to-string atom))
71 (t (error "Bad shqq atom: %S" atom))))
73 (defun shqq--quote-atom (atom)
74 (shqq--quote-string (shqq--atom-to-string atom)))
76 (defun shqq--match-comma (form)
77 "Matches FORM against ,foo i.e. (\, foo) and returns foo.
78 Returns nil if FORM didn't match. You can't disambiguate between
79 FORM matching ,nil and not matching."
86 (defun shqq--match-comma2 (form)
87 "Matches FORM against ,,foo i.e. (\, (\, foo)) and returns foo.
88 Returns nil if FORM didn't match. You can't disambiguate between
89 FORM matching ,,nil and not matching."
94 (shqq--match-comma (cadr form))))
97 (defmacro shqq (parts)
98 "First, PARTS is turned into a list of strings. For this,
99 every element of PARTS must be one of:
101 - a symbol, evaluating to its name,
103 - a string, evaluating to itself,
105 - a number, evaluating to its decimal representation,
107 - \",expr\", where EXPR must evaluate to an atom that will be
108 interpreted according to the previous rules,
110 - \",@list-expr\", where LIST-EXPR must evaluate to a list whose
111 elements will each be interpreted like the EXPR in an \",EXPR\"
112 form, and spliced into the list of strings,
114 - \",,expr\", where EXPR is interpreted like in \",expr\",
116 - or \",,@expr\", where EXPR is interpreted like in \",@expr\".
118 In the resulting list of strings, all elements except the ones
119 resulting from \",,expr\" and \",,@expr\" forms are quoted for
122 Finally, the resulting list of strings is concatenated with
128 ((atom part) (shqq--quote-atom part))
129 ;; We use the match-comma helpers because pcase can't match ,foo.
131 ;; ,,foo i.e. (, (, foo))
132 ((pred shqq--match-comma2)
133 (shqq--match-comma2 part))
134 ;; ,,@foo i.e. (, (,@ foo))
135 ((and (pred shqq--match-comma)
136 (let `,@,form (shqq--match-comma part)))
137 `(mapconcat #'identity ,form " "))
139 ;; Insert redundant 'and x' to work around debbugs#18554.
140 ((and x (pred shqq--match-comma))
141 `(shqq--quote-atom ,(shqq--match-comma part)))
144 `(mapconcat #'shqq--quote-atom ,form " "))
146 (error "Bad shqq part: %S" part))))))
148 `(mapconcat #'identity (list ,@parts) " ")))
150 (provide 'shell-quasiquote)
154 ;;; shell-quasiquote.el ends here