module PIPEasp_Templates {

import from PIPEasp_PortType all;
import from PIPEasp_Types all;

template (value) ASP_PExecute ts_PExecute(template (value) charstring command,
					  template (value) charstring stdin) := {
	command := command,
	stdin := stdin
}

template (present) ASP_PResult tr_PResult(template (present) charstring stdout,
					  template (present) charstring stderr,
					  template (present) integer code) := {
	stdout := stdout,
	stderr := stderr,
	code   := code
}

template (value) ASP_PExecuteBackground ts_ExecBg(charstring cmd) := {
	command := cmd
}

template (present) ASP_PStdout tr_Stdout(template (present) charstring line) := {
	stdout := line
}

template (present) ASP_PStderr tr_Stderr(template (present) charstring line) := {
	stderr := line
}

template (value) ASP_PStdin ts_Stdin(template (value) charstring line) := {
	stdin :=line
}

/* Ignore output from stderr: */
altstep as_ignore_stderr(PIPEasp_PT pt) {
[] pt.receive(tr_Stderr(?)) { repeat; }
}

/* User should map(component_name:PIPE, system:PIPE) before using this function. */
function f_PIPEasp_exec_sync_PResult(PIPEasp_PT pt,
				    charstring cmdline,
				    template (present) ASP_PResult res_exp := tr_PResult(?,?,0),
				    float time_out := 10.0) return ASP_PResult {
	var ASP_PResult res;
	timer t;

	if (time_out > 0.0) {
		t.start(time_out);
	}

	log ("Executing: ", cmdline);
	pt.send(ts_PExecute(cmdline, ""));

	alt {
	[] pt.receive(res_exp) -> value res;
	[time_out > 0.0] t.timeout {
		setverdict(fail, "Timeout: ", cmdline);
		mtc.stop;
	}
	}
	log ("Result: ", res);
	return res;
}

function f_PIPEasp_exec_sync(PIPEasp_PT pt,
			     charstring cmdline,
			     template (present) integer rc := 0) return ASP_PResult {
	return f_PIPEasp_exec_sync_PResult(pt, cmdline, tr_PResult(?, ?, rc));
}

}
