#!/usr/local/bin/perl
# run.cgi
# Run a command, and maybe display it's output

require './proc-lib.pl';
&ReadParse();
$access{'run'} || &error($text{'run_ecannot'});
&switch_acl_uid();

$in{'input'} =~ s/\r//g;
if ($in{'mode'}) {
	# fork and run..
	if (!($pid = fork())) {
		close(STDIN); close(STDOUT); close(STDERR);
		open(PROC, "| ($in{'cmd'})");
		print PROC $in{'input'};
		close(PROC);
		exit;
		}
	&redirect("index_tree.cgi");
	}
else {
	# run and display output..
	&ui_print_unbuffered_header(undef, $text{'run_title'}, "");
	print "<p>\n";
	print &text('run_output', "<tt>$in{'cmd'}</tt>"),"<p>\n";
	print "<pre>";
	$got = &safe_process_exec_logged($in{'cmd'}, 0, 0,
					 STDOUT, $in{'input'}, 1);
	if (!$got) { print "<i>$text{'run_none'}</i>\n"; }
	print "</pre>\n";
	&ui_print_footer("", $text{'index'});
	}
&webmin_log("run", undef, undef, \%in);

