ob-shell.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ;;; ob-shell.el --- org-babel functions for shell evaluation
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating shell source code.
  19. ;;; Code:
  20. (require 'ob)
  21. (require 'shell)
  22. (eval-when-compile (require 'cl))
  23. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  24. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  25. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  26. (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
  27. (declare-function orgtbl-to-generic "org-table" (table params))
  28. (defvar org-babel-default-header-args:shell '())
  29. (defcustom org-babel-shell-names
  30. '("sh" "bash" "csh" "ash" "dash" "ksh" "mksh" "posh")
  31. "List of names of shell supported by babel shell code blocks."
  32. :group 'org-babel
  33. :type 'string
  34. :initialize
  35. (lambda (symbol value)
  36. (set-default symbol (second value))
  37. (mapc
  38. (lambda (name)
  39. (eval `(defun ,(intern (concat "org-babel-execute:" name)) (body params)
  40. ,(format "Execute a block of %s commands with Babel." name)
  41. (let ((shell-file-name ,name))
  42. (org-babel-execute:shell body params)))))
  43. (second value))))
  44. (defun org-babel-execute:shell (body params)
  45. "Execute a block of Shell commands with Babel.
  46. This function is called by `org-babel-execute-src-block'."
  47. (let* ((session (org-babel-sh-initiate-session
  48. (cdr (assoc :session params))))
  49. (stdin (let ((stdin (cdr (assoc :stdin params))))
  50. (when stdin (org-babel-sh-var-to-string
  51. (org-babel-ref-resolve stdin)))))
  52. (cmdline (cdr (assoc :cmdline params)))
  53. (full-body (org-babel-expand-body:generic
  54. body params (org-babel-variable-assignments:shell params))))
  55. (org-babel-reassemble-table
  56. (org-babel-sh-evaluate session full-body params stdin cmdline)
  57. (org-babel-pick-name
  58. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  59. (org-babel-pick-name
  60. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  61. (defun org-babel-prep-session:shell (session params)
  62. "Prepare SESSION according to the header arguments specified in PARAMS."
  63. (let* ((session (org-babel-sh-initiate-session session))
  64. (var-lines (org-babel-variable-assignments:shell params)))
  65. (org-babel-comint-in-buffer session
  66. (mapc (lambda (var)
  67. (insert var) (comint-send-input nil t)
  68. (org-babel-comint-wait-for-output session)) var-lines))
  69. session))
  70. (defun org-babel-load-session:shell (session body params)
  71. "Load BODY into SESSION."
  72. (save-window-excursion
  73. (let ((buffer (org-babel-prep-session:shell session params)))
  74. (with-current-buffer buffer
  75. (goto-char (process-mark (get-buffer-process (current-buffer))))
  76. (insert (org-babel-chomp body)))
  77. buffer)))
  78. ;; helper functions
  79. (defun org-babel-variable-assignments:sh-generic
  80. (varname values &optional sep hline)
  81. "Returns a list of statements declaring the values as a generic variable."
  82. (format "%s=%s" varname (org-babel-sh-var-to-sh values sep hline)))
  83. (defun org-babel-variable-assignments:bash_array
  84. (varname values &optional sep hline)
  85. "Returns a list of statements declaring the values as a bash array."
  86. (format "unset %s\ndeclare -a %s=( %s )"
  87. varname varname
  88. (mapconcat
  89. (lambda (value) (org-babel-sh-var-to-sh value sep hline))
  90. values
  91. " ")))
  92. (defun org-babel-variable-assignments:bash_assoc
  93. (varname values &optional sep hline)
  94. "Returns a list of statements declaring the values as bash associative array."
  95. (format "unset %s\ndeclare -A %s\n%s"
  96. varname varname
  97. (mapconcat
  98. (lambda (items)
  99. (format "%s[%s]=%s"
  100. varname
  101. (org-babel-sh-var-to-sh (car items) sep hline)
  102. (org-babel-sh-var-to-sh (cdr items) sep hline)))
  103. values
  104. "\n")))
  105. (defun org-babel-variable-assignments:bash (varname values &optional sep hline)
  106. "Represents the parameters as useful Bash shell variables."
  107. (if (listp values)
  108. (if (and (listp (car values)) (= 1 (length (car values))))
  109. (org-babel-variable-assignments:bash_array varname values sep hline)
  110. (org-babel-variable-assignments:bash_assoc varname values sep hline))
  111. (org-babel-variable-assignments:sh-generic varname values sep hline)))
  112. (defun org-babel-variable-assignments:shell (params)
  113. "Return list of shell statements assigning the block's variables."
  114. (let ((sep (cdr (assoc :separator params)))
  115. (hline (when (string= "yes" (cdr (assoc :hlines params)))
  116. (or (cdr (assoc :hline-string params))
  117. "hline"))))
  118. (mapcar
  119. (lambda (pair)
  120. (if (string-match "bash$" shell-file-name)
  121. (org-babel-variable-assignments:bash
  122. (car pair) (cdr pair) sep hline)
  123. (org-babel-variable-assignments:sh-generic
  124. (car pair) (cdr pair) sep hline)))
  125. (mapcar #'cdr (org-babel-get-header params :var)))))
  126. (defun org-babel-sh-var-to-sh (var &optional sep hline)
  127. "Convert an elisp value to a shell variable.
  128. Convert an elisp var into a string of shell commands specifying a
  129. var of the same value."
  130. (concat "'" (replace-regexp-in-string
  131. "'" "'\"'\"'"
  132. (org-babel-sh-var-to-string var sep hline))
  133. "'"))
  134. (defun org-babel-sh-var-to-string (var &optional sep hline)
  135. "Convert an elisp value to a string."
  136. (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
  137. (cond
  138. ((and (listp var) (or (listp (car var)) (equal (car var) 'hline)))
  139. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var
  140. :hline hline)))
  141. ((listp var)
  142. (mapconcat echo-var var "\n"))
  143. (t (funcall echo-var var)))))
  144. (defun org-babel-sh-initiate-session (&optional session params)
  145. "Initiate a session named SESSION according to PARAMS."
  146. (when (and session (not (string= session "none")))
  147. (save-window-excursion
  148. (or (org-babel-comint-buffer-livep session)
  149. (progn
  150. (shell session)
  151. ;; Needed for Emacs 23 since the marker is initially
  152. ;; undefined and the filter functions try to use it without
  153. ;; checking.
  154. (set-marker comint-last-output-start (point))
  155. (get-buffer (current-buffer)))))))
  156. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  157. "String to indicate that evaluation has completed.")
  158. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  159. "String to indicate that evaluation has completed.")
  160. (defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
  161. "Pass BODY to the Shell process in BUFFER.
  162. If RESULT-TYPE equals `output' then return a list of the outputs
  163. of the statements in BODY, if RESULT-TYPE equals `value' then
  164. return the value of the last statement in BODY."
  165. (let ((results
  166. (cond
  167. ((or stdin cmdline) ; external shell script w/STDIN
  168. (let ((script-file (org-babel-temp-file "sh-script-"))
  169. (stdin-file (org-babel-temp-file "sh-stdin-"))
  170. (shebang (cdr (assoc :shebang params)))
  171. (padline (not (string= "no" (cdr (assoc :padline params))))))
  172. (with-temp-file script-file
  173. (when shebang (insert (concat shebang "\n")))
  174. (when padline (insert "\n"))
  175. (insert body))
  176. (set-file-modes script-file #o755)
  177. (with-temp-file stdin-file (insert (or stdin "")))
  178. (with-temp-buffer
  179. (call-process-shell-command
  180. (concat (if shebang script-file
  181. (format "%s %s" shell-file-name script-file))
  182. (and cmdline (concat " " cmdline)))
  183. stdin-file
  184. (current-buffer))
  185. (buffer-string))))
  186. (session ; session evaluation
  187. (mapconcat
  188. #'org-babel-sh-strip-weird-long-prompt
  189. (mapcar
  190. #'org-babel-trim
  191. (butlast
  192. (org-babel-comint-with-output
  193. (session org-babel-sh-eoe-output t body)
  194. (mapc
  195. (lambda (line)
  196. (insert line)
  197. (comint-send-input nil t)
  198. (while (save-excursion
  199. (goto-char comint-last-input-end)
  200. (not (re-search-forward
  201. comint-prompt-regexp nil t)))
  202. (accept-process-output
  203. (get-buffer-process (current-buffer)))))
  204. (append
  205. (split-string (org-babel-trim body) "\n")
  206. (list org-babel-sh-eoe-indicator))))
  207. 2)) "\n"))
  208. ('otherwise ; external shell script
  209. (if (and (cdr (assoc :shebang params))
  210. (> (length (cdr (assoc :shebang params))) 0))
  211. (let ((script-file (org-babel-temp-file "sh-script-"))
  212. (shebang (cdr (assoc :shebang params)))
  213. (padline (not (equal "no" (cdr (assoc :padline params))))))
  214. (with-temp-file script-file
  215. (when shebang (insert (concat shebang "\n")))
  216. (when padline (insert "\n"))
  217. (insert body))
  218. (set-file-modes script-file #o755)
  219. (org-babel-eval script-file ""))
  220. (org-babel-eval shell-file-name (org-babel-trim body)))))))
  221. (when results
  222. (let ((result-params (cdr (assoc :result-params params))))
  223. (org-babel-result-cond result-params
  224. results
  225. (let ((tmp-file (org-babel-temp-file "sh-")))
  226. (with-temp-file tmp-file (insert results))
  227. (org-babel-import-elisp-from-file tmp-file)))))))
  228. (defun org-babel-sh-strip-weird-long-prompt (string)
  229. "Remove prompt cruft from a string of shell output."
  230. (while (string-match "^% +[\r\n$]+ *" string)
  231. (setq string (substring string (match-end 0))))
  232. string)
  233. (provide 'ob-shell)
  234. ;;; ob-shell.el ends here