1 ;;; ob-shell.el --- Babel Functions for Shell Evaluation -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating shell source code.
34 (declare-function org-babel-comint-in-buffer
"ob-comint" (buffer &rest body
)
36 (declare-function org-babel-comint-wait-for-output
"ob-comint" (buffer))
37 (declare-function org-babel-comint-buffer-livep
"ob-comint" (buffer))
38 (declare-function org-babel-comint-with-output
"ob-comint" (meta &rest body
)
40 (declare-function orgtbl-to-generic
"org-table" (table params
))
42 (defvar org-babel-default-header-args
:shell
'())
43 (defvar org-babel-shell-names
)
45 (defun org-babel-shell-initialize ()
46 "Define execution functions associated to shell names.
47 This function has to be called whenever `org-babel-shell-names'
48 is modified outside the Customize interface."
50 (dolist (name org-babel-shell-names
)
51 (eval `(defun ,(intern (concat "org-babel-execute:" name
))
53 ,(format "Execute a block of %s commands with Babel." name
)
54 (let ((shell-file-name ,name
))
55 (org-babel-execute:shell body params
))))
56 (eval `(defalias ',(intern (concat "org-babel-variable-assignments:" name
))
57 'org-babel-variable-assignments
:shell
58 ,(format "Return list of %s statements assigning to the block's \
62 (defcustom org-babel-shell-names
63 '("sh" "bash" "zsh" "fish" "csh" "ash" "dash" "ksh" "mksh" "posh")
64 "List of names of shell supported by babel shell code blocks.
65 Call `org-babel-shell-initialize' when modifying this variable
66 outside the Customize interface."
68 :type
'(repeat (string :tag
"Shell name: "))
69 :set
(lambda (symbol value
)
70 (set-default symbol value
)
71 (org-babel-shell-initialize)))
73 (defun org-babel-execute:shell
(body params
)
74 "Execute a block of Shell commands with Babel.
75 This function is called by `org-babel-execute-src-block'."
76 (let* ((session (org-babel-sh-initiate-session
77 (cdr (assq :session params
))))
78 (stdin (let ((stdin (cdr (assq :stdin params
))))
79 (when stdin
(org-babel-sh-var-to-string
80 (org-babel-ref-resolve stdin
)))))
81 (cmdline (cdr (assq :cmdline params
)))
82 (full-body (org-babel-expand-body:generic
83 body params
(org-babel-variable-assignments:shell params
))))
84 (org-babel-reassemble-table
85 (org-babel-sh-evaluate session full-body params stdin cmdline
)
87 (cdr (assq :colname-names params
)) (cdr (assq :colnames params
)))
89 (cdr (assq :rowname-names params
)) (cdr (assq :rownames params
))))))
91 (defun org-babel-prep-session:shell
(session params
)
92 "Prepare SESSION according to the header arguments specified in PARAMS."
93 (let* ((session (org-babel-sh-initiate-session session
))
94 (var-lines (org-babel-variable-assignments:shell params
)))
95 (org-babel-comint-in-buffer session
97 (insert var
) (comint-send-input nil t
)
98 (org-babel-comint-wait-for-output session
)) var-lines
))
101 (defun org-babel-load-session:shell
(session body params
)
102 "Load BODY into SESSION."
103 (save-window-excursion
104 (let ((buffer (org-babel-prep-session:shell session params
)))
105 (with-current-buffer buffer
106 (goto-char (process-mark (get-buffer-process (current-buffer))))
107 (insert (org-babel-chomp body
)))
112 (defun org-babel--variable-assignments:sh-generic
113 (varname values
&optional sep hline
)
114 "Returns a list of statements declaring the values as a generic variable."
115 (format "%s=%s" varname
(org-babel-sh-var-to-sh values sep hline
)))
117 (defun org-babel--variable-assignments:bash_array
118 (varname values
&optional sep hline
)
119 "Returns a list of statements declaring the values as a bash array."
120 (format "unset %s\ndeclare -a %s=( %s )"
123 (lambda (value) (org-babel-sh-var-to-sh value sep hline
))
127 (defun org-babel--variable-assignments:bash_assoc
128 (varname values
&optional sep hline
)
129 "Returns a list of statements declaring the values as bash associative array."
130 (format "unset %s\ndeclare -A %s\n%s"
136 (org-babel-sh-var-to-sh (car items
) sep hline
)
137 (org-babel-sh-var-to-sh (cdr items
) sep hline
)))
141 (defun org-babel--variable-assignments:bash
(varname values
&optional sep hline
)
142 "Represents the parameters as useful Bash shell variables."
144 (`((,_
,_ .
,_
) .
,_
) ;two-dimensional array
145 (org-babel--variable-assignments:bash_assoc varname values sep hline
))
146 (`(,_ .
,_
) ;simple list
147 (org-babel--variable-assignments:bash_array varname values sep hline
))
149 (org-babel--variable-assignments:sh-generic varname values sep hline
))))
151 (defun org-babel-variable-assignments:shell
(params)
152 "Return list of shell statements assigning the block's variables."
153 (let ((sep (cdr (assq :separator params
)))
154 (hline (when (string= "yes" (cdr (assq :hlines params
)))
155 (or (cdr (assq :hline-string params
))
159 (if (string-suffix-p "bash" shell-file-name
)
160 (org-babel--variable-assignments:bash
161 (car pair
) (cdr pair
) sep hline
)
162 (org-babel--variable-assignments:sh-generic
163 (car pair
) (cdr pair
) sep hline
)))
164 (org-babel--get-vars params
))))
166 (defun org-babel-sh-var-to-sh (var &optional sep hline
)
167 "Convert an elisp value to a shell variable.
168 Convert an elisp var into a string of shell commands specifying a
169 var of the same value."
170 (concat "'" (replace-regexp-in-string
172 (org-babel-sh-var-to-string var sep hline
))
175 (defun org-babel-sh-var-to-string (var &optional sep hline
)
176 "Convert an elisp value to a string."
177 (let ((echo-var (lambda (v) (if (stringp v
) v
(format "%S" v
)))))
179 ((and (listp var
) (or (listp (car var
)) (eq (car var
) 'hline
)))
180 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt echo-var
183 (mapconcat echo-var var
"\n"))
184 (t (funcall echo-var var
)))))
186 (defun org-babel-sh-initiate-session (&optional session _params
)
187 "Initiate a session named SESSION according to PARAMS."
188 (when (and session
(not (string= session
"none")))
189 (save-window-excursion
190 (or (org-babel-comint-buffer-livep session
)
193 ;; Needed for Emacs 23 since the marker is initially
194 ;; undefined and the filter functions try to use it without
196 (set-marker comint-last-output-start
(point))
197 (get-buffer (current-buffer)))))))
199 (defvar org-babel-sh-eoe-indicator
"echo 'org_babel_sh_eoe'"
200 "String to indicate that evaluation has completed.")
201 (defvar org-babel-sh-eoe-output
"org_babel_sh_eoe"
202 "String to indicate that evaluation has completed.")
204 (defun org-babel-sh-evaluate (session body
&optional params stdin cmdline
)
205 "Pass BODY to the Shell process in BUFFER.
206 If RESULT-TYPE equals `output' then return a list of the outputs
207 of the statements in BODY, if RESULT-TYPE equals `value' then
208 return the value of the last statement in BODY."
209 (let* ((shebang (cdr (assq :shebang params
)))
212 ((or stdin cmdline
) ; external shell script w/STDIN
213 (let ((script-file (org-babel-temp-file "sh-script-"))
214 (stdin-file (org-babel-temp-file "sh-stdin-"))
215 (padline (not (string= "no" (cdr (assq :padline params
))))))
216 (with-temp-file script-file
217 (when shebang
(insert shebang
"\n"))
218 (when padline
(insert "\n"))
220 (set-file-modes script-file
#o755
)
221 (with-temp-file stdin-file
(insert (or stdin
"")))
223 (call-process-shell-command
224 (concat (if shebang script-file
225 (format "%s %s" shell-file-name script-file
))
226 (and cmdline
(concat " " cmdline
)))
230 (session ; session evaluation
232 #'org-babel-sh-strip-weird-long-prompt
236 (org-babel-comint-with-output
237 (session org-babel-sh-eoe-output t body
)
238 (dolist (line (append (split-string (org-trim body
) "\n")
239 (list org-babel-sh-eoe-indicator
)))
241 (comint-send-input nil t
)
242 (while (save-excursion
243 (goto-char comint-last-input-end
)
244 (not (re-search-forward
245 comint-prompt-regexp nil t
)))
246 (accept-process-output
247 (get-buffer-process (current-buffer))))))
250 ;; External shell script, with or without a predefined
252 ((org-string-nw-p shebang
)
253 (let ((script-file (org-babel-temp-file "sh-script-"))
254 (padline (not (equal "no" (cdr (assq :padline params
))))))
255 (with-temp-file script-file
256 (insert shebang
"\n")
257 (when padline
(insert "\n"))
259 (set-file-modes script-file
#o755
)
260 (org-babel-eval script-file
"")))
262 (org-babel-eval shell-file-name
(org-trim body
))))))
264 (let ((result-params (cdr (assq :result-params params
))))
265 (org-babel-result-cond result-params
267 (let ((tmp-file (org-babel-temp-file "sh-")))
268 (with-temp-file tmp-file
(insert results
))
269 (org-babel-import-elisp-from-file tmp-file
)))))))
271 (defun org-babel-sh-strip-weird-long-prompt (string)
272 "Remove prompt cruft from a string of shell output."
273 (while (string-match "^% +[\r\n$]+ *" string
)
274 (setq string
(substring string
(match-end 0))))
281 ;;; ob-shell.el ends here