Function: eshell/cat

eshell/cat is a byte-compiled function defined in em-unix.el.gz.

Signature

(eshell/cat &rest ARGS)

Documentation

Implementation of cat in Lisp.

If in a pipeline, or the file is not a regular file, directory or symlink, then revert to the system's definition of cat.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/em-unix.el.gz
(defun eshell/cat (&rest args)
  "Implementation of cat in Lisp.
If in a pipeline, or the file is not a regular file, directory or
symlink, then revert to the system's definition of cat."
  (setq args (eshell-stringify-list (flatten-tree args)))
  (if (or eshell-in-pipeline-p
	  (catch 'special
	    (dolist (arg args)
	      (unless (or (and (stringp arg)
			       (> (length arg) 0)
			       (eq (aref arg 0) ?-))
			  (let ((attrs (eshell-file-attributes arg)))
			    (and attrs
				 (memq (aref (file-attribute-modes attrs) 0)
					     '(?d ?l ?-)))))
		(throw 'special t)))))
      (let ((ext-cat (eshell-search-path "cat")))
	(if ext-cat
	    (throw 'eshell-external (eshell-external-command ext-cat args))
	  (if eshell-in-pipeline-p
	      (error "Eshell's `cat' does not work in pipelines")
	    (error "Eshell's `cat' cannot display one of the files given"))))
    (eshell-eval-using-options
     "cat" args
     '((?h "help" nil nil "show this usage screen")
       :external "cat"
       :show-usage
       :usage "[OPTION] FILE...
Concatenate FILE(s), or standard input, to standard output.")
     (dolist (file args)
       (if (string= file "-")
	   (throw 'eshell-external
		  (eshell-external-command "cat" args))))
     (let ((curbuf (current-buffer)))
       (eshell-with-buffered-print
         (dolist (file args)
	   (with-temp-buffer
	     (insert-file-contents file)
	     (goto-char (point-min))
             (while (not (eobp))
               (let* ((pos (min (+ (point) eshell-buffered-print-size)
                                (point-max)))
                      (str (buffer-substring (point) pos)))
                 (with-current-buffer curbuf
                   (eshell-buffered-print str))
                 (goto-char pos))))))))))