1 ;;; ob-shell.el --- Babel Functions for Shell Evaluation -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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 <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating shell source code.
31 (eval-when-compile (require 'cl
))
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
'())
44 (defcustom org-babel-shell-names
45 '("sh" "bash" "csh" "ash" "dash" "ksh" "mksh" "posh")
46 "List of names of shell supported by babel shell code blocks."
48 :type
'(repeat (string :tag
"Shell name: "))
49 :set
(lambda (symbol value
)
50 (set-default symbol value
)
52 (eval `(defun ,(intern (concat "org-babel-execute:" name
))
54 ,(format "Execute a block of %s commands with Babel." name
)
55 (let ((shell-file-name ,name
))
56 (org-babel-execute:shell body params
)))
59 (defun org-babel-execute:shell
(body params
)
60 "Execute a block of Shell commands with Babel.
61 This function is called by `org-babel-execute-src-block'."
62 (let* ((session (org-babel-sh-initiate-session
63 (cdr (assoc :session params
))))
64 (stdin (let ((stdin (cdr (assoc :stdin params
))))
65 (when stdin
(org-babel-sh-var-to-string
66 (org-babel-ref-resolve stdin
)))))
67 (cmdline (cdr (assoc :cmdline params
)))
68 (full-body (org-babel-expand-body:generic
69 body params
(org-babel-variable-assignments:shell params
))))
70 (org-babel-reassemble-table
71 (org-babel-sh-evaluate session full-body params stdin cmdline
)
73 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
75 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
77 (defun org-babel-prep-session:shell
(session params
)
78 "Prepare SESSION according to the header arguments specified in PARAMS."
79 (let* ((session (org-babel-sh-initiate-session session
))
80 (var-lines (org-babel-variable-assignments:shell params
)))
81 (org-babel-comint-in-buffer session
83 (insert var
) (comint-send-input nil t
)
84 (org-babel-comint-wait-for-output session
)) var-lines
))
87 (defun org-babel-load-session:shell
(session body params
)
88 "Load BODY into SESSION."
89 (save-window-excursion
90 (let ((buffer (org-babel-prep-session:shell session params
)))
91 (with-current-buffer buffer
92 (goto-char (process-mark (get-buffer-process (current-buffer))))
93 (insert (org-babel-chomp body
)))
97 (defun org-babel-variable-assignments:sh-generic
98 (varname values
&optional sep hline
)
99 "Returns a list of statements declaring the values as a generic variable."
100 (format "%s=%s" varname
(org-babel-sh-var-to-sh values sep hline
)))
102 (defun org-babel-variable-assignments:bash_array
103 (varname values
&optional sep hline
)
104 "Returns a list of statements declaring the values as a bash array."
105 (format "unset %s\ndeclare -a %s=( %s )"
108 (lambda (value) (org-babel-sh-var-to-sh value sep hline
))
112 (defun org-babel-variable-assignments:bash_assoc
113 (varname values
&optional sep hline
)
114 "Returns a list of statements declaring the values as bash associative array."
115 (format "unset %s\ndeclare -A %s\n%s"
121 (org-babel-sh-var-to-sh (car items
) sep hline
)
122 (org-babel-sh-var-to-sh (cdr items
) sep hline
)))
126 (defun org-babel-variable-assignments:bash
(varname values
&optional sep hline
)
127 "Represents the parameters as useful Bash shell variables."
129 (if (and (listp (car values
)) (= 1 (length (car values
))))
130 (org-babel-variable-assignments:bash_array varname values sep hline
)
131 (org-babel-variable-assignments:bash_assoc varname values sep hline
))
132 (org-babel-variable-assignments:sh-generic varname values sep hline
)))
134 (defun org-babel-variable-assignments:shell
(params)
135 "Return list of shell statements assigning the block's variables."
136 (let ((sep (cdr (assoc :separator params
)))
137 (hline (when (string= "yes" (cdr (assoc :hlines params
)))
138 (or (cdr (assoc :hline-string params
))
142 (if (string-match "bash$" shell-file-name
)
143 (org-babel-variable-assignments:bash
144 (car pair
) (cdr pair
) sep hline
)
145 (org-babel-variable-assignments:sh-generic
146 (car pair
) (cdr pair
) sep hline
)))
147 (org-babel--get-vars params
))))
149 (defun org-babel-sh-var-to-sh (var &optional sep hline
)
150 "Convert an elisp value to a shell variable.
151 Convert an elisp var into a string of shell commands specifying a
152 var of the same value."
153 (concat "'" (replace-regexp-in-string
155 (org-babel-sh-var-to-string var sep hline
))
158 (defun org-babel-sh-var-to-string (var &optional sep hline
)
159 "Convert an elisp value to a string."
160 (let ((echo-var (lambda (v) (if (stringp v
) v
(format "%S" v
)))))
162 ((and (listp var
) (or (listp (car var
)) (equal (car var
) 'hline
)))
163 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt echo-var
166 (mapconcat echo-var var
"\n"))
167 (t (funcall echo-var var
)))))
169 (defun org-babel-sh-initiate-session (&optional session _params
)
170 "Initiate a session named SESSION according to PARAMS."
171 (when (and session
(not (string= session
"none")))
172 (save-window-excursion
173 (or (org-babel-comint-buffer-livep session
)
176 ;; Needed for Emacs 23 since the marker is initially
177 ;; undefined and the filter functions try to use it without
179 (set-marker comint-last-output-start
(point))
180 (get-buffer (current-buffer)))))))
182 (defvar org-babel-sh-eoe-indicator
"echo 'org_babel_sh_eoe'"
183 "String to indicate that evaluation has completed.")
184 (defvar org-babel-sh-eoe-output
"org_babel_sh_eoe"
185 "String to indicate that evaluation has completed.")
187 (defun org-babel-sh-evaluate (session body
&optional params stdin cmdline
)
188 "Pass BODY to the Shell process in BUFFER.
189 If RESULT-TYPE equals `output' then return a list of the outputs
190 of the statements in BODY, if RESULT-TYPE equals `value' then
191 return the value of the last statement in BODY."
194 ((or stdin cmdline
) ; external shell script w/STDIN
195 (let ((script-file (org-babel-temp-file "sh-script-"))
196 (stdin-file (org-babel-temp-file "sh-stdin-"))
197 (shebang (cdr (assoc :shebang params
)))
198 (padline (not (string= "no" (cdr (assoc :padline params
))))))
199 (with-temp-file script-file
200 (when shebang
(insert (concat shebang
"\n")))
201 (when padline
(insert "\n"))
203 (set-file-modes script-file
#o755
)
204 (with-temp-file stdin-file
(insert (or stdin
"")))
206 (call-process-shell-command
207 (concat (if shebang script-file
208 (format "%s %s" shell-file-name script-file
))
209 (and cmdline
(concat " " cmdline
)))
213 (session ; session evaluation
215 #'org-babel-sh-strip-weird-long-prompt
219 (org-babel-comint-with-output
220 (session org-babel-sh-eoe-output t body
)
224 (comint-send-input nil t
)
225 (while (save-excursion
226 (goto-char comint-last-input-end
)
227 (not (re-search-forward
228 comint-prompt-regexp nil t
)))
229 (accept-process-output
230 (get-buffer-process (current-buffer)))))
232 (split-string (org-trim body
) "\n")
233 (list org-babel-sh-eoe-indicator
))))
235 ('otherwise
; external shell script
236 (if (and (cdr (assoc :shebang params
))
237 (> (length (cdr (assoc :shebang params
))) 0))
238 (let ((script-file (org-babel-temp-file "sh-script-"))
239 (shebang (cdr (assoc :shebang params
)))
240 (padline (not (equal "no" (cdr (assoc :padline params
))))))
241 (with-temp-file script-file
242 (when shebang
(insert (concat shebang
"\n")))
243 (when padline
(insert "\n"))
245 (set-file-modes script-file
#o755
)
246 (org-babel-eval script-file
""))
247 (org-babel-eval shell-file-name
(org-trim body
)))))))
249 (let ((result-params (cdr (assoc :result-params params
))))
250 (org-babel-result-cond result-params
252 (let ((tmp-file (org-babel-temp-file "sh-")))
253 (with-temp-file tmp-file
(insert results
))
254 (org-babel-import-elisp-from-file tmp-file
)))))))
256 (defun org-babel-sh-strip-weird-long-prompt (string)
257 "Remove prompt cruft from a string of shell output."
258 (while (string-match "^% +[\r\n$]+ *" string
)
259 (setq string
(substring string
(match-end 0))))
266 ;;; ob-shell.el ends here