1 ;;; ob-shell.el --- org-babel functions for shell evaluation
3 ;; Copyright (C) 2009-2014 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
))
34 (declare-function org-babel-comint-wait-for-output
"ob-comint" (buffer))
35 (declare-function org-babel-comint-buffer-livep
"ob-comint" (buffer))
36 (declare-function org-babel-comint-with-output
"ob-comint" (meta &rest body
))
37 (declare-function orgtbl-to-generic
"org-table" (table params
))
39 (defvar org-babel-default-header-args
:sh
'())
41 (defcustom org-babel-sh-command shell-file-name
42 "Command used to invoke a shell.
43 Set by default to the value of `shell-file-name'. This will be
44 passed to `shell-command-on-region'"
48 (defcustom org-babel-sh-var-quote-fmt
49 "$(cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n)"
50 "Format string used to escape variables when passed to shell scripts."
54 (defcustom org-babel-shell-names
55 '("sh" "bash" "csh" "ash" "dash" "ksh" "mksh" "posh")
56 "List of names of shell supported by babel shell code blocks."
60 (lambda (symbol value
)
61 (set-default symbol
(second value
))
64 (eval `(defun ,(intern (concat "org-babel-execute:" name
)) (body params
)
65 ,(format "Execute a block of %s commands with Babel." name
)
66 (let ((org-babel-sh-command ,name
))
67 (org-babel-execute:shell body params
)))))
70 (defun org-babel-execute:shell
(body params
)
71 "Execute a block of Shell commands with Babel.
72 This function is called by `org-babel-execute-src-block'."
73 (let* ((session (org-babel-sh-initiate-session
74 (cdr (assoc :session params
))))
75 (stdin (let ((stdin (cdr (assoc :stdin params
))))
76 (when stdin
(org-babel-sh-var-to-string
77 (org-babel-ref-resolve stdin
)))))
78 (full-body (org-babel-expand-body:generic
79 body params
(org-babel-variable-assignments:sh params
))))
80 (org-babel-reassemble-table
81 (org-babel-sh-evaluate session full-body params stdin
)
83 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
85 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
87 (defun org-babel-prep-session:sh
(session params
)
88 "Prepare SESSION according to the header arguments specified in PARAMS."
89 (let* ((session (org-babel-sh-initiate-session session
))
90 (var-lines (org-babel-variable-assignments:sh params
)))
91 (org-babel-comint-in-buffer session
93 (insert var
) (comint-send-input nil t
)
94 (org-babel-comint-wait-for-output session
)) var-lines
))
97 (defun org-babel-load-session:sh
(session body params
)
98 "Load BODY into SESSION."
99 (save-window-excursion
100 (let ((buffer (org-babel-prep-session:sh session params
)))
101 (with-current-buffer buffer
102 (goto-char (process-mark (get-buffer-process (current-buffer))))
103 (insert (org-babel-chomp body
)))
108 (defun org-babel-variable-assignments:sh
(params)
109 "Return list of shell statements assigning the block's variables."
110 (let ((sep (cdr (assoc :separator params
))))
115 (org-babel-sh-var-to-sh (cdr pair
) sep
)))
116 (mapcar #'cdr
(org-babel-get-header params
:var
)))))
118 (defun org-babel-sh-var-to-sh (var &optional sep
)
119 "Convert an elisp value to a shell variable.
120 Convert an elisp var into a string of shell commands specifying a
121 var of the same value."
122 (format org-babel-sh-var-quote-fmt
(org-babel-sh-var-to-string var sep
)))
124 (defun org-babel-sh-var-to-string (var &optional sep
)
125 "Convert an elisp value to a string."
126 (let ((echo-var (lambda (v) (if (stringp v
) v
(format "%S" v
)))))
128 ((and (listp var
) (or (listp (car var
)) (equal (car var
) 'hline
)))
129 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt echo-var
)))
131 (mapconcat echo-var var
"\n"))
132 (t (funcall echo-var var
)))))
134 (defun org-babel-sh-table-or-results (results)
135 "Convert RESULTS to an appropriate elisp value.
136 If the results look like a table, then convert them into an
137 Emacs-lisp table, otherwise return the results as a string."
138 (org-babel-script-escape results
))
140 (defun org-babel-sh-initiate-session (&optional session params
)
141 "Initiate a session named SESSION according to PARAMS."
142 (when (and session
(not (string= session
"none")))
143 (save-window-excursion
144 (or (org-babel-comint-buffer-livep session
)
145 (progn (shell session
) (get-buffer (current-buffer)))))))
147 (defvar org-babel-sh-eoe-indicator
"echo 'org_babel_sh_eoe'"
148 "String to indicate that evaluation has completed.")
149 (defvar org-babel-sh-eoe-output
"org_babel_sh_eoe"
150 "String to indicate that evaluation has completed.")
152 (defun org-babel-sh-evaluate (session body
&optional params stdin
)
153 "Pass BODY to the Shell process in BUFFER.
154 If RESULT-TYPE equals 'output then return a list of the outputs
155 of the statements in BODY, if RESULT-TYPE equals 'value then
156 return the value of the last statement in BODY."
159 (stdin ; external shell script w/STDIN
160 (let ((script-file (org-babel-temp-file "sh-script-"))
161 (stdin-file (org-babel-temp-file "sh-stdin-"))
162 (shebang (cdr (assoc :shebang params
)))
163 (padline (not (string= "no" (cdr (assoc :padline params
))))))
164 (with-temp-file script-file
165 (when shebang
(insert (concat shebang
"\n")))
166 (when padline
(insert "\n"))
168 (set-file-modes script-file
#o755
)
169 (with-temp-file stdin-file
(insert stdin
))
171 (call-process-shell-command
174 (format "%s %s" org-babel-sh-command script-file
))
178 (session ; session evaluation
180 #'org-babel-sh-strip-weird-long-prompt
184 (org-babel-comint-with-output
185 (session org-babel-sh-eoe-output t body
)
189 (comint-send-input nil t
)
190 (while (save-excursion
191 (goto-char comint-last-input-end
)
192 (not (re-search-forward
193 comint-prompt-regexp nil t
)))
194 (accept-process-output
195 (get-buffer-process (current-buffer)))))
197 (split-string (org-babel-trim body
) "\n")
198 (list org-babel-sh-eoe-indicator
))))
200 ('otherwise
; external shell script
201 (if (and (cdr (assoc :shebang params
))
202 (> (length (cdr (assoc :shebang params
))) 0))
203 (let ((script-file (org-babel-temp-file "sh-script-"))
204 (shebang (cdr (assoc :shebang params
)))
205 (padline (not (equal "no" (cdr (assoc :padline params
))))))
206 (with-temp-file script-file
207 (when shebang
(insert (concat shebang
"\n")))
208 (when padline
(insert "\n"))
210 (set-file-modes script-file
#o755
)
211 (org-babel-eval script-file
""))
212 (org-babel-eval org-babel-sh-command
(org-babel-trim body
)))))))
214 (let ((result-params (cdr (assoc :result-params params
))))
215 (org-babel-result-cond result-params
217 (let ((tmp-file (org-babel-temp-file "sh-")))
218 (with-temp-file tmp-file
(insert results
))
219 (org-babel-import-elisp-from-file tmp-file
)))))))
221 (defun org-babel-sh-strip-weird-long-prompt (string)
222 "Remove prompt cruft from a string of shell output."
223 (while (string-match "^% +[\r\n$]+ *" string
)
224 (setq string
(substring string
(match-end 0))))
231 ;;; ob-shell.el ends here