
(*
 * RunThrough routes an expression through a command and returns the
 * result.  Optional arguments go to the Open command (e.g., to set the
 * GraphicsRendering).
 *
 * (bruce 12 Oct 90 -- modified for 2.0)
 *)


RunThrough::usage = "RunThrough[\"command\", expr] executes an external
command, giving the printed form of expr as input, and taking the output,
reading it as Mathematica input, and returning the result."

(*
 * Externally visible objects: RunThrough
 *)

Begin["`Private`"]

(*
 * StreamFileName[stream] returns the filename part of a stream indicator,
 * and works in either 1.2 or 2.0. (bruce 17 Sep 90)
 *)

StreamFileName[streamhead_Symbol[name_String, serialnumber_Integer]] := name

StreamFileName[name_String] := name

RunThrough[cmd_String, expr_] :=
	Block[{
		`tmpfile = OpenTemporary[]
		},
		Write[tmpfile, expr];
		WriteString[tmpfile, "\n"];
		Close[tmpfile];
		Get[
			ToString[
				StringForm[
					"! (rm `1`; `2`) <`1`",
						StreamFileName[tmpfile],
						cmd
				]
			]
		]
	]

End[]

Protect[RunThrough]

Null
