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: http://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.
33 (declare-function org-babel-comint-in-buffer
"ob-comint" (buffer &rest body
)
35 (declare-function org-babel-comint-wait-for-output
"ob-comint" (buffer))
36 (declare-function org-babel-comint-buffer-livep
"ob-comint" (buffer))
37 (declare-function org-babel-comint-with-output
"ob-comint" (meta &rest body
)
39 (declare-function org-trim
"org" (s &optional keep-lead
))
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" "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."
211 ((or stdin cmdline
) ; external shell script w/STDIN
212 (let ((script-file (org-babel-temp-file "sh-script-"))
213 (stdin-file (org-babel-temp-file "sh-stdin-"))
214 (shebang (cdr (assq :shebang params
)))
215 (padline (not (string= "no" (cdr (assq :padline params
))))))
216 (with-temp-file script-file
217 (when shebang
(insert (concat 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
)
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)))))
249 (split-string (org-trim body
) "\n")
250 (list org-babel-sh-eoe-indicator
))))
252 ('otherwise
; external shell script
253 (if (and (cdr (assq :shebang params
))
254 (> (length (cdr (assq :shebang params
))) 0))
255 (let ((script-file (org-babel-temp-file "sh-script-"))
256 (shebang (cdr (assq :shebang params
)))
257 (padline (not (equal "no" (cdr (assq :padline params
))))))
258 (with-temp-file script-file
259 (when shebang
(insert (concat shebang
"\n")))
260 (when padline
(insert "\n"))
262 (set-file-modes script-file
#o755
)
263 (org-babel-eval script-file
""))
264 (org-babel-eval shell-file-name
(org-trim body
)))))))
266 (let ((result-params (cdr (assq :result-params params
))))
267 (org-babel-result-cond result-params
269 (let ((tmp-file (org-babel-temp-file "sh-")))
270 (with-temp-file tmp-file
(insert results
))
271 (org-babel-import-elisp-from-file tmp-file
)))))))
273 (defun org-babel-sh-strip-weird-long-prompt (string)
274 "Remove prompt cruft from a string of shell output."
275 (while (string-match "^% +[\r\n$]+ *" string
)
276 (setq string
(substring string
(match-end 0))))
283 ;;; ob-shell.el ends here