ob-sh.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ;;; ob-sh.el --- org-babel functions for shell evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.7
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating shell source code.
  20. ;;; Code:
  21. (require 'ob)
  22. (require 'ob-ref)
  23. (require 'ob-comint)
  24. (require 'ob-eval)
  25. (require 'shell)
  26. (eval-when-compile (require 'cl))
  27. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  28. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  29. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  30. (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
  31. (declare-function orgtbl-to-generic "org-table" (table params))
  32. (defvar org-babel-default-header-args:sh '())
  33. (defvar org-babel-sh-command "sh"
  34. "Command used to invoke a shell.
  35. This will be passed to `shell-command-on-region'")
  36. (defcustom org-babel-sh-var-quote-fmt
  37. "$(cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n)"
  38. "Format string used to escape variables when passed to shell scripts."
  39. :group 'org-babel
  40. :type 'string)
  41. (defun org-babel-execute:sh (body params)
  42. "Execute a block of Shell commands with Babel.
  43. This function is called by `org-babel-execute-src-block'."
  44. (let* ((session (org-babel-sh-initiate-session
  45. (cdr (assoc :session params))))
  46. (result-params (cdr (assoc :result-params params)))
  47. (stdin ((lambda (stdin) (when stdin (org-babel-sh-var-to-string
  48. (org-babel-ref-resolve stdin))))
  49. (cdr (assoc :stdin params))))
  50. (full-body (org-babel-expand-body:generic
  51. body params (org-babel-variable-assignments:sh params))))
  52. (org-babel-reassemble-table
  53. (org-babel-sh-evaluate session full-body result-params stdin)
  54. (org-babel-pick-name
  55. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  56. (org-babel-pick-name
  57. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  58. (defun org-babel-prep-session:sh (session params)
  59. "Prepare SESSION according to the header arguments specified in PARAMS."
  60. (let* ((session (org-babel-sh-initiate-session session))
  61. (var-lines (org-babel-variable-assignments:sh params)))
  62. (org-babel-comint-in-buffer session
  63. (mapc (lambda (var)
  64. (insert var) (comint-send-input nil t)
  65. (org-babel-comint-wait-for-output session)) var-lines))
  66. session))
  67. (defun org-babel-load-session:sh (session body params)
  68. "Load BODY into SESSION."
  69. (save-window-excursion
  70. (let ((buffer (org-babel-prep-session:sh session params)))
  71. (with-current-buffer buffer
  72. (goto-char (process-mark (get-buffer-process (current-buffer))))
  73. (insert (org-babel-chomp body)))
  74. buffer)))
  75. ;; helper functions
  76. (defun org-babel-variable-assignments:sh (params)
  77. "Return list of shell statements assigning the block's variables"
  78. (let ((sep (cdr (assoc :separator params))))
  79. (mapcar
  80. (lambda (pair)
  81. (format "%s=%s"
  82. (car pair)
  83. (org-babel-sh-var-to-sh (cdr pair) sep)))
  84. (mapcar #'cdr (org-babel-get-header params :var)))))
  85. (defun org-babel-sh-var-to-sh (var &optional sep)
  86. "Convert an elisp value to a shell variable.
  87. Convert an elisp var into a string of shell commands specifying a
  88. var of the same value."
  89. (format org-babel-sh-var-quote-fmt (org-babel-sh-var-to-string var sep)))
  90. (defun org-babel-sh-var-to-string (var &optional sep)
  91. "Convert an elisp value to a string."
  92. (flet ((echo-var (v) (if (stringp v) v (format "%S" v))))
  93. (cond
  94. ((and (listp var) (listp (car var)))
  95. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var)))
  96. ((listp var)
  97. (mapconcat #'echo-var var "\n"))
  98. (t (echo-var var)))))
  99. (defun org-babel-sh-table-or-results (results)
  100. "Convert RESULTS to an appropriate elisp value.
  101. If the results look like a table, then convert them into an
  102. Emacs-lisp table, otherwise return the results as a string."
  103. (org-babel-script-escape results))
  104. (defun org-babel-sh-initiate-session (&optional session params)
  105. "Initiate a session named SESSION according to PARAMS."
  106. (when (and session (not (string= session "none")))
  107. (save-window-excursion
  108. (or (org-babel-comint-buffer-livep session)
  109. (progn (shell session) (get-buffer (current-buffer)))))))
  110. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  111. "String to indicate that evaluation has completed.")
  112. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  113. "String to indicate that evaluation has completed.")
  114. (defun org-babel-sh-evaluate (session body &optional result-params stdin)
  115. "Pass BODY to the Shell process in BUFFER.
  116. If RESULT-TYPE equals 'output then return a list of the outputs
  117. of the statements in BODY, if RESULT-TYPE equals 'value then
  118. return the value of the last statement in BODY."
  119. ((lambda (results)
  120. (when results
  121. (if (or (member "scalar" result-params)
  122. (member "verbatim" result-params)
  123. (member "output" result-params))
  124. results
  125. (let ((tmp-file (org-babel-temp-file "sh-")))
  126. (with-temp-file tmp-file (insert results))
  127. (org-babel-import-elisp-from-file tmp-file)))))
  128. (cond
  129. (stdin ; external shell script w/STDIN
  130. (let ((script-file (org-babel-temp-file "sh-script-"))
  131. (stdin-file (org-babel-temp-file "sh-stdin-")))
  132. (with-temp-file script-file (insert body))
  133. (with-temp-file stdin-file (insert stdin))
  134. (with-temp-buffer
  135. (call-process-shell-command
  136. (format "%s %s" org-babel-sh-command script-file)
  137. stdin-file
  138. (current-buffer))
  139. (buffer-string))))
  140. (session ; session evaluation
  141. (mapconcat
  142. #'org-babel-sh-strip-weird-long-prompt
  143. (mapcar
  144. #'org-babel-trim
  145. (butlast
  146. (org-babel-comint-with-output
  147. (session org-babel-sh-eoe-output t body)
  148. (mapc
  149. (lambda (line)
  150. (insert line)
  151. (comint-send-input nil t)
  152. (while (save-excursion
  153. (goto-char comint-last-input-end)
  154. (not (re-search-forward
  155. comint-prompt-regexp nil t)))
  156. (accept-process-output (get-buffer-process (current-buffer)))))
  157. (append
  158. (split-string (org-babel-trim body) "\n")
  159. (list org-babel-sh-eoe-indicator))))
  160. 2)) "\n"))
  161. ('otherwise ; external shell script
  162. (org-babel-eval org-babel-sh-command (org-babel-trim body))))))
  163. (defun org-babel-sh-strip-weird-long-prompt (string)
  164. "Remove prompt cruft from a string of shell output."
  165. (while (string-match "^% +[\r\n$]+ *" string)
  166. (setq string (substring string (match-end 0))))
  167. string)
  168. (provide 'ob-sh)
  169. ;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
  170. ;;; ob-sh.el ends here