esh-io.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. ;;; esh-io.el --- I/O management
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; At the moment, only output redirection is supported in Eshell. To
  17. ;; use input redirection, the following syntax will work, assuming
  18. ;; that the command after the pipe is always an external command:
  19. ;;
  20. ;; cat <file> | <command>
  21. ;;
  22. ;; Otherwise, output redirection and piping are provided in a manner
  23. ;; consistent with most shells. Therefore, only unique features are
  24. ;; mentioned here.
  25. ;;
  26. ;;;_* Insertion
  27. ;;
  28. ;; To insert at the location of point in a buffer, use '>>>':
  29. ;;
  30. ;; echo alpha >>> #<buffer *scratch*>;
  31. ;;
  32. ;;;_* Pseudo-devices
  33. ;;
  34. ;; A few pseudo-devices are provided, since Emacs cannot write
  35. ;; directly to a UNIX device file:
  36. ;;
  37. ;; echo alpha > /dev/null ; the bit bucket
  38. ;; echo alpha > /dev/kill ; set the kill ring
  39. ;; echo alpha >> /dev/clip ; append to the clipboard
  40. ;;
  41. ;;;_* Multiple output targets
  42. ;;
  43. ;; Eshell can write to multiple output targets, including pipes.
  44. ;; Example:
  45. ;;
  46. ;; (+ 1 2) > a > b > c ; prints number to all three files
  47. ;; (+ 1 2) > a | wc ; prints to 'a', and pipes to 'wc'
  48. ;;; Code:
  49. (provide 'esh-io)
  50. (eval-when-compile
  51. (require 'cl)
  52. (require 'eshell))
  53. (defgroup eshell-io nil
  54. "Eshell's I/O management code provides a scheme for treating many
  55. different kinds of objects -- symbols, files, buffers, etc. -- as
  56. though they were files."
  57. :tag "I/O management"
  58. :group 'eshell)
  59. ;;; User Variables:
  60. (defcustom eshell-io-load-hook nil
  61. "A hook that gets run when `eshell-io' is loaded."
  62. :version "24.1" ; removed eshell-io-initialize
  63. :type 'hook
  64. :group 'eshell-io)
  65. (defcustom eshell-number-of-handles 3
  66. "The number of file handles that eshell supports.
  67. Currently this is standard input, output and error. But even all of
  68. these Emacs does not currently support with asynchronous processes
  69. \(which is what eshell uses so that you can continue doing work in
  70. other buffers) ."
  71. :type 'integer
  72. :group 'eshell-io)
  73. (defcustom eshell-output-handle 1
  74. "The index of the standard output handle."
  75. :type 'integer
  76. :group 'eshell-io)
  77. (defcustom eshell-error-handle 2
  78. "The index of the standard error handle."
  79. :type 'integer
  80. :group 'eshell-io)
  81. (defcustom eshell-buffer-shorthand nil
  82. "If non-nil, a symbol name can be used for a buffer in redirection.
  83. If nil, redirecting to a buffer requires buffer name syntax. If this
  84. variable is set, redirection directly to Lisp symbols will be
  85. impossible.
  86. Example:
  87. echo hello > '*scratch* ; works if `eshell-buffer-shorthand' is t
  88. echo hello > #<buffer *scratch*> ; always works"
  89. :type 'boolean
  90. :group 'eshell-io)
  91. (defcustom eshell-print-queue-size 5
  92. "The size of the print queue, for doing buffered printing.
  93. This is basically a speed enhancement, to avoid blocking the Lisp code
  94. from executing while Emacs is redisplaying."
  95. :type 'integer
  96. :group 'eshell-io)
  97. (defcustom eshell-virtual-targets
  98. '(("/dev/eshell" eshell-interactive-print nil)
  99. ("/dev/kill" (lambda (mode)
  100. (if (eq mode 'overwrite)
  101. (kill-new ""))
  102. 'eshell-kill-append) t)
  103. ("/dev/clip" (lambda (mode)
  104. (if (eq mode 'overwrite)
  105. (let ((x-select-enable-clipboard t))
  106. (kill-new "")))
  107. 'eshell-clipboard-append) t))
  108. "Map virtual devices name to Emacs Lisp functions.
  109. If the user specifies any of the filenames above as a redirection
  110. target, the function in the second element will be called.
  111. If the third element is non-nil, the redirection mode is passed as an
  112. argument (which is the symbol `overwrite', `append' or `insert'), and
  113. the function is expected to return another function -- which is the
  114. output function. Otherwise, the second element itself is the output
  115. function.
  116. The output function is then called repeatedly with single strings,
  117. which represents successive pieces of the output of the command, until nil
  118. is passed, meaning EOF.
  119. NOTE: /dev/null is handled specially as a virtual target, and should
  120. not be added to this variable."
  121. :type '(repeat
  122. (list (string :tag "Target")
  123. function
  124. (choice (const :tag "Func returns output-func" t)
  125. (const :tag "Func is output-func" nil))))
  126. :group 'eshell-io)
  127. (put 'eshell-virtual-targets 'risky-local-variable t)
  128. ;;; Internal Variables:
  129. (defvar eshell-current-handles nil)
  130. (defvar eshell-last-command-status 0
  131. "The exit code from the last command. 0 if successful.")
  132. (defvar eshell-last-command-result nil
  133. "The result of the last command. Not related to success.")
  134. (defvar eshell-output-file-buffer nil
  135. "If non-nil, the current buffer is a file output buffer.")
  136. (defvar eshell-print-count)
  137. (defvar eshell-current-redirections)
  138. ;;; Functions:
  139. (defun eshell-io-initialize ()
  140. "Initialize the I/O subsystem code."
  141. (add-hook 'eshell-parse-argument-hook
  142. 'eshell-parse-redirection nil t)
  143. (make-local-variable 'eshell-current-redirections)
  144. (add-hook 'eshell-pre-rewrite-command-hook
  145. 'eshell-strip-redirections nil t)
  146. (add-hook 'eshell-post-rewrite-command-hook
  147. 'eshell-apply-redirections nil t))
  148. (defun eshell-parse-redirection ()
  149. "Parse an output redirection, such as '2>'."
  150. (if (and (not eshell-current-quoted)
  151. (looking-at "\\([0-9]\\)?\\(<\\|>+\\)&?\\([0-9]\\)?\\s-*"))
  152. (if eshell-current-argument
  153. (eshell-finish-arg)
  154. (let ((sh (match-string 1))
  155. (oper (match-string 2))
  156. ; (th (match-string 3))
  157. )
  158. (if (string= oper "<")
  159. (error "Eshell does not support input redirection"))
  160. (eshell-finish-arg
  161. (prog1
  162. (list 'eshell-set-output-handle
  163. (or (and sh (string-to-number sh)) 1)
  164. (list 'quote
  165. (aref [overwrite append insert]
  166. (1- (length oper)))))
  167. (goto-char (match-end 0))))))))
  168. (defun eshell-strip-redirections (terms)
  169. "Rewrite any output redirections in TERMS."
  170. (setq eshell-current-redirections (list t))
  171. (let ((tl terms)
  172. (tt (cdr terms)))
  173. (while tt
  174. (if (not (and (consp (car tt))
  175. (eq (caar tt) 'eshell-set-output-handle)))
  176. (setq tt (cdr tt)
  177. tl (cdr tl))
  178. (unless (cdr tt)
  179. (error "Missing redirection target"))
  180. (nconc eshell-current-redirections
  181. (list (list 'ignore
  182. (append (car tt) (list (cadr tt))))))
  183. (setcdr tl (cddr tt))
  184. (setq tt (cddr tt))))
  185. (setq eshell-current-redirections
  186. (cdr eshell-current-redirections))))
  187. (defun eshell-apply-redirections (cmdsym)
  188. "Apply any redirection which were specified for COMMAND."
  189. (if eshell-current-redirections
  190. (set cmdsym
  191. (append (list 'progn)
  192. eshell-current-redirections
  193. (list (symbol-value cmdsym))))))
  194. (defun eshell-create-handles
  195. (standard-output output-mode &optional standard-error error-mode)
  196. "Create a new set of file handles for a command.
  197. The default location for standard output and standard error will go to
  198. STANDARD-OUTPUT and STANDARD-ERROR, respectively.
  199. OUTPUT-MODE and ERROR-MODE are either `overwrite', `append' or `insert';
  200. a nil value of mode defaults to `insert'."
  201. (let ((handles (make-vector eshell-number-of-handles nil))
  202. (output-target (eshell-get-target standard-output output-mode))
  203. (error-target (eshell-get-target standard-error error-mode)))
  204. (aset handles eshell-output-handle (cons output-target 1))
  205. (if standard-error
  206. (aset handles eshell-error-handle (cons error-target 1))
  207. (aset handles eshell-error-handle (cons output-target 1)))
  208. handles))
  209. (defun eshell-protect-handles (handles)
  210. "Protect the handles in HANDLES from a being closed."
  211. (let ((idx 0))
  212. (while (< idx eshell-number-of-handles)
  213. (if (aref handles idx)
  214. (setcdr (aref handles idx)
  215. (1+ (cdr (aref handles idx)))))
  216. (setq idx (1+ idx))))
  217. handles)
  218. (defun eshell-close-target (target status)
  219. "Close an output TARGET, passing STATUS as the result.
  220. STATUS should be non-nil on successful termination of the output."
  221. (cond
  222. ((symbolp target) nil)
  223. ;; If we were redirecting to a file, save the file and close the
  224. ;; buffer.
  225. ((markerp target)
  226. (let ((buf (marker-buffer target)))
  227. (when buf ; somebody's already killed it!
  228. (save-current-buffer
  229. (set-buffer buf)
  230. (when eshell-output-file-buffer
  231. (save-buffer)
  232. (when (eq eshell-output-file-buffer t)
  233. (or status (set-buffer-modified-p nil))
  234. (kill-buffer buf)))))))
  235. ;; If we're redirecting to a process (via a pipe, or process
  236. ;; redirection), send it EOF so that it knows we're finished.
  237. ((eshell-processp target)
  238. (if (eq (process-status target) 'run)
  239. (process-send-eof target)))
  240. ;; A plain function redirection needs no additional arguments
  241. ;; passed.
  242. ((functionp target)
  243. (funcall target status))
  244. ;; But a more complicated function redirection (which can only
  245. ;; happen with aliases at the moment) has arguments that need to be
  246. ;; passed along with it.
  247. ((consp target)
  248. (apply (car target) status (cdr target)))))
  249. (defun eshell-close-handles (exit-code &optional result handles)
  250. "Close all of the current handles, taking refcounts into account.
  251. EXIT-CODE is the process exit code; mainly, it is zero, if the command
  252. completed successfully. RESULT is the quoted value of the last
  253. command. If nil, then the meta variables for keeping track of the
  254. last execution result should not be changed."
  255. (let ((idx 0))
  256. (assert (or (not result) (eq (car result) 'quote)))
  257. (setq eshell-last-command-status exit-code
  258. eshell-last-command-result (cadr result))
  259. (while (< idx eshell-number-of-handles)
  260. (let ((handles (or handles eshell-current-handles)))
  261. (when (aref handles idx)
  262. (setcdr (aref handles idx)
  263. (1- (cdr (aref handles idx))))
  264. (when (= (cdr (aref handles idx)) 0)
  265. (let ((target (car (aref handles idx))))
  266. (if (not (listp target))
  267. (eshell-close-target target (= exit-code 0))
  268. (while target
  269. (eshell-close-target (car target) (= exit-code 0))
  270. (setq target (cdr target)))))
  271. (setcar (aref handles idx) nil))))
  272. (setq idx (1+ idx)))
  273. nil))
  274. (defun eshell-kill-append (string)
  275. "Call `kill-append' with STRING, if it is indeed a string."
  276. (if (stringp string)
  277. (kill-append string nil)))
  278. (defun eshell-clipboard-append (string)
  279. "Call `kill-append' with STRING, if it is indeed a string."
  280. (if (stringp string)
  281. (let ((x-select-enable-clipboard t))
  282. (kill-append string nil))))
  283. (defun eshell-get-target (target &optional mode)
  284. "Convert TARGET, which is a raw argument, into a valid output target.
  285. MODE is either `overwrite', `append' or `insert'; if it is omitted or nil,
  286. it defaults to `insert'."
  287. (setq mode (or mode 'insert))
  288. (cond
  289. ((stringp target)
  290. (let ((redir (assoc target eshell-virtual-targets)))
  291. (if redir
  292. (if (nth 2 redir)
  293. (funcall (nth 1 redir) mode)
  294. (nth 1 redir))
  295. (let* ((exists (get-file-buffer target))
  296. (buf (find-file-noselect target t)))
  297. (with-current-buffer buf
  298. (if buffer-file-read-only
  299. (error "Cannot write to read-only file `%s'" target))
  300. (setq buffer-read-only nil)
  301. (set (make-local-variable 'eshell-output-file-buffer)
  302. (if (eq exists buf) 0 t))
  303. (cond ((eq mode 'overwrite)
  304. (erase-buffer))
  305. ((eq mode 'append)
  306. (goto-char (point-max))))
  307. (point-marker))))))
  308. ((or (bufferp target)
  309. (and (boundp 'eshell-buffer-shorthand)
  310. (symbol-value 'eshell-buffer-shorthand)
  311. (symbolp target)
  312. (not (memq target '(t nil)))))
  313. (let ((buf (if (bufferp target)
  314. target
  315. (get-buffer-create
  316. (symbol-name target)))))
  317. (with-current-buffer buf
  318. (cond ((eq mode 'overwrite)
  319. (erase-buffer))
  320. ((eq mode 'append)
  321. (goto-char (point-max))))
  322. (point-marker))))
  323. ((functionp target) nil)
  324. ((symbolp target)
  325. (if (eq mode 'overwrite)
  326. (set target nil))
  327. target)
  328. ((or (eshell-processp target)
  329. (markerp target))
  330. target)
  331. (t
  332. (error "Invalid redirection target: %s"
  333. (eshell-stringify target)))))
  334. (defvar grep-null-device)
  335. (defun eshell-set-output-handle (index mode &optional target)
  336. "Set handle INDEX, using MODE, to point to TARGET."
  337. (when target
  338. (if (and (stringp target)
  339. (or (cond
  340. ((boundp 'null-device)
  341. (string= target null-device))
  342. ((boundp 'grep-null-device)
  343. (string= target grep-null-device))
  344. (t nil))
  345. (string= target "/dev/null")))
  346. (aset eshell-current-handles index nil)
  347. (let ((where (eshell-get-target target mode))
  348. (current (car (aref eshell-current-handles index))))
  349. (if (and (listp current)
  350. (not (member where current)))
  351. (setq current (append current (list where)))
  352. (setq current (list where)))
  353. (if (not (aref eshell-current-handles index))
  354. (aset eshell-current-handles index (cons nil 1)))
  355. (setcar (aref eshell-current-handles index) current)))))
  356. (defun eshell-interactive-output-p ()
  357. "Return non-nil if current handles are bound for interactive display."
  358. (and (eq (car (aref eshell-current-handles
  359. eshell-output-handle)) t)
  360. (eq (car (aref eshell-current-handles
  361. eshell-error-handle)) t)))
  362. (defvar eshell-print-queue nil)
  363. (defvar eshell-print-queue-count -1)
  364. (defsubst eshell-print (object)
  365. "Output OBJECT to the standard output handle."
  366. (eshell-output-object object eshell-output-handle))
  367. (defun eshell-flush (&optional reset-p)
  368. "Flush out any lines that have been queued for printing.
  369. Must be called before printing begins with -1 as its argument, and
  370. after all printing is over with no argument."
  371. (ignore
  372. (if reset-p
  373. (setq eshell-print-queue nil
  374. eshell-print-queue-count reset-p)
  375. (if eshell-print-queue
  376. (eshell-print eshell-print-queue))
  377. (eshell-flush 0))))
  378. (defun eshell-init-print-buffer ()
  379. "Initialize the buffered printing queue."
  380. (eshell-flush -1))
  381. (defun eshell-buffered-print (&rest strings)
  382. "A buffered print -- *for strings only*."
  383. (if (< eshell-print-queue-count 0)
  384. (progn
  385. (eshell-print (apply 'concat strings))
  386. (setq eshell-print-queue-count 0))
  387. (if (= eshell-print-queue-count eshell-print-queue-size)
  388. (eshell-flush))
  389. (setq eshell-print-queue
  390. (concat eshell-print-queue (apply 'concat strings))
  391. eshell-print-queue-count (1+ eshell-print-queue-count))))
  392. (defsubst eshell-error (object)
  393. "Output OBJECT to the standard error handle."
  394. (eshell-output-object object eshell-error-handle))
  395. (defsubst eshell-errorn (object)
  396. "Output OBJECT followed by a newline to the standard error handle."
  397. (eshell-error object)
  398. (eshell-error "\n"))
  399. (defsubst eshell-printn (object)
  400. "Output OBJECT followed by a newline to the standard output handle."
  401. (eshell-print object)
  402. (eshell-print "\n"))
  403. (defun eshell-output-object-to-target (object target)
  404. "Insert OBJECT into TARGET.
  405. Returns what was actually sent, or nil if nothing was sent."
  406. (cond
  407. ((functionp target)
  408. (funcall target object))
  409. ((symbolp target)
  410. (if (eq target t) ; means "print to display"
  411. (eshell-output-filter nil (eshell-stringify object))
  412. (if (not (symbol-value target))
  413. (set target object)
  414. (setq object (eshell-stringify object))
  415. (if (not (stringp (symbol-value target)))
  416. (set target (eshell-stringify
  417. (symbol-value target))))
  418. (set target (concat (symbol-value target) object)))))
  419. ((markerp target)
  420. (if (buffer-live-p (marker-buffer target))
  421. (with-current-buffer (marker-buffer target)
  422. (let ((moving (= (point) target)))
  423. (save-excursion
  424. (goto-char target)
  425. (unless (stringp object)
  426. (setq object (eshell-stringify object)))
  427. (insert-and-inherit object)
  428. (set-marker target (point-marker)))
  429. (if moving
  430. (goto-char target))))))
  431. ((eshell-processp target)
  432. (when (eq (process-status target) 'run)
  433. (unless (stringp object)
  434. (setq object (eshell-stringify object)))
  435. (process-send-string target object)))
  436. ((consp target)
  437. (apply (car target) object (cdr target))))
  438. object)
  439. (defun eshell-output-object (object &optional handle-index handles)
  440. "Insert OBJECT, using HANDLE-INDEX specifically)."
  441. (let ((target (car (aref (or handles eshell-current-handles)
  442. (or handle-index eshell-output-handle)))))
  443. (if (and target (not (listp target)))
  444. (eshell-output-object-to-target object target)
  445. (while target
  446. (eshell-output-object-to-target object (car target))
  447. (setq target (cdr target))))))
  448. ;;; esh-io.el ends here