Function: tramp-expand-script

tramp-expand-script is a byte-compiled function defined in tramp-sh.el.gz.

Signature

(tramp-expand-script VEC SCRIPT)

Documentation

Expand SCRIPT with remote files or commands.

"%a", "%h", "%l", "%m", "%o", "%p", "%q", "%r", "%s"
and "%y" format specifiers are replaced by the respective awk, hexdump, ls, test, od', perl, test -e, readlink, stat and python commands. "%n" is replaced by "2>/dev/null", and "%t" is replaced by a temporary file name. If VEC is nil, the respective local commands are used. If there is a format specifier which cannot be expanded, this function returns nil.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-sh.el.gz
;;; Internal Functions:

(defun tramp-expand-script (vec script)
  "Expand SCRIPT with remote files or commands.
\"%a\", \"%h\", \"%l\", \"%m\", \"%o\", \"%p\", \"%q\", \"%r\", \"%s\"
and \"%y\" format specifiers are replaced by the respective `awk',
`hexdump', `ls', `test', od', `perl', `test -e', `readlink', `stat' and
`python' commands.  \"%n\" is replaced by \"2>/dev/null\", and \"%t\" is
replaced by a temporary file name.  If VEC is nil, the respective local
commands are used.  If there is a format specifier which cannot be
expanded, this function returns nil."
  (if (not (string-match-p
	    (rx (| bol (not "%")) "%" (any "ahlmnopqrsty")) script))
      script
    (catch 'wont-work
      (let ((awk (when (string-match-p (rx (| bol (not "%")) "%a") script)
		   (or
		    (if vec (tramp-get-remote-awk vec) (executable-find "awk"))
		    (throw 'wont-work nil))))
	    (hdmp (when (string-match-p (rx (| bol (not "%")) "%h") script)
		    (or
		     (if vec (tramp-get-remote-hexdump vec)
		       (executable-find "hexdump"))
		     (throw 'wont-work nil))))
	    (dev (when (string-match-p (rx (| bol (not "%")) "%n") script)
		   (or
		    (if vec (concat "2>" (tramp-get-remote-null-device vec))
		      (if (eq system-type 'windows-nt) ""
			(concat "2>" null-device)))
		    (throw 'wont-work nil))))
	    (ls (when (string-match-p (rx (| bol (not "%")) "%l") script)
		  (format "%s %s"
			  (or (tramp-get-ls-command vec)
			      (throw 'wont-work nil))
			  (tramp-sh--quoting-style-options vec))))
	    (test (when (string-match-p (rx (| bol (not "%")) "%m") script)
		    (or (tramp-get-test-command vec)
			(throw 'wont-work nil))))
	    (test-e (when (string-match-p (rx (| bol (not "%")) "%q") script)
		      (or (tramp-get-file-exists-command vec)
			  (throw 'wont-work nil))))
	    (od (when (string-match-p (rx (| bol (not "%")) "%o") script)
		  (or (if vec (tramp-get-remote-od vec) (executable-find "od"))
		      (throw 'wont-work nil))))
	    (perl (when (string-match-p (rx (| bol (not "%")) "%p") script)
		    (or
		     (if vec
			 (tramp-get-remote-perl vec) (executable-find "perl"))
		     (throw 'wont-work nil))))
	    (python (when (string-match-p (rx (| bol (not "%")) "%y") script)
		      (or
		       (if vec
			   (tramp-get-remote-python vec)
			 (executable-find "python"))
		       (throw 'wont-work nil))))
	    (readlink (when (string-match-p (rx (| bol (not "%")) "%r") script)
			(format "%s %s"
				(or
				 (if vec
				     (tramp-get-remote-readlink vec)
				   (executable-find "readlink"))
				 (throw 'wont-work nil))
				"--canonicalize-missing")))
	    (stat (when (string-match-p (rx (| bol (not "%")) "%s") script)
		    (or
		     (if vec
			 (tramp-get-remote-stat vec) (executable-find "stat"))
		     (throw 'wont-work nil))))
	    (tmp (when (string-match-p (rx (| bol (not "%")) "%t") script)
		   (or
		    (if vec
			(tramp-file-local-name (tramp-make-tramp-temp-name vec))
		      (tramp-compat-make-temp-name))
		    (throw 'wont-work nil)))))
	(format-spec
	 script
	 (format-spec-make
	  ?a awk ?h hdmp ?l ls ?m test ?n dev ?o od ?p perl
	  ?q test-e ?r readlink ?s stat ?t tmp ?y python))))))