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-replace-command
		   (eshell-parse-command (eshell-quote-argument 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-init-print-buffer)
    (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)))
       (dolist (file args)
	 (with-temp-buffer
	   (insert-file-contents file)
	   (goto-char (point-min))
	   (while (not (eobp))
	     (let ((str (buffer-substring
			 (point) (min (1+ (line-end-position))
				      (point-max)))))
	       (with-current-buffer curbuf
		 (eshell-buffered-print str)))
	     (forward-line)))))
     (eshell-flush)
     ;; if the file does not end in a newline, do not emit one
     (setq eshell-ensure-newline-p nil))))