Add cmpSysExecEventXXX

This commit is contained in:
cmp167
2023-09-06 02:51:19 +12:00
parent 8301ee83d3
commit fe2e8b11a3

View File

@@ -264,8 +264,25 @@
"?" => "\\?" , "!" => "\\!" "?" => "\\?" , "!" => "\\!"
); );
if(@$str[0] == "asis") do {
return @$str[1]; if(!is_array($str))
break;
if(!isset($str[0]))
return null;
if($str[0] != "asis") {
$this->d("Unknown modifier '".$str[0]."'");
return null;
}
if(!isset($str[1]) || !$str[1]) {
$this->d("Invalid string '".$str[1]."'");
return null;
}
return $str[1];
} while(0);
return strtr($str, $ttr); return strtr($str, $ttr);
} }
@@ -274,7 +291,13 @@
$cmaEsc = array(); $cmaEsc = array();
for($i = 0; $i < count($cmaRaw); $i++) { for($i = 0; $i < count($cmaRaw); $i++) {
$cmaEsc[] = $this->cmpShellEscStr($cmaRaw[$i]); $str = $this->cmpShellEscStr($cmaRaw[$i]);
if(!$str) {
return null;
}
$cmaEsc[] = $str;
} }
return $cmaEsc; return $cmaEsc;
@@ -289,78 +312,96 @@
return $this->cmpShellEscStr($cmaRaw); return $this->cmpShellEscStr($cmaRaw);
} }
function cmpSysExec($cmaRaw, $opt = []) { function cmpSysExecEventString(&$prs, &$buf, &$opt) {
$cmdStr = $this->cmpShellEscStr($cmaRaw); $fs = "/\n/";
if(!@$opt["silent"]) { if(isset($opt["eventStringDelimer"]) && $opt["eventStringDelimer"])
$this->d("cmpSysExec: " . $cmdStr); $fs = $opt["eventStringDelimer"];
$a = preg_split($fs, $buf);
if(!$a) {
$this->d("Can't split buffer");
return false;
} }
$fdsAll = array( $c = count($a) - 1;
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$workDir = "/tmp"; if($c < 1)
return true;
if(@$opt["workDir"]) for($i = 0; $i < $c; $i++) {
$workDir = $opt["workDir"]; $this->d($prs["streamCurrent"] . ": " . $a[$i]);
$envAll = null; if($prs["streamCurrent"] == "stdout") {
if(isset($opt["eventStringStdOut"]) && $opt["eventStringStdOut"]) {
$ppsAll = array(); ($opt["eventStringStdOut"]["call"])($a[$i], $prs);
}
$prcRes = proc_open($cmdStr, $fdsAll, $ppsAll, $workDir, $envAll); }
else if($prs["streamCurrent"] == "stderr") {
if(!is_resource($prcRes)) { if(isset($opt["eventStringStdErr"]) && $opt["eventStringStdErr"]) {
$this->e("Can't create process"); ($opt["eventStringStdErr"]["call"])($a[$i], $prs);
return null; }
}
} }
$blk = 1; // $this->d("Stream tail: " . $a[$c]);
$buf = $a[$c];
if(isset($opt["blocking"])) { return true;
$blk = $opt["blocking"];
} }
stream_set_blocking($ppsAll[1], $blk); function cmpSysExecEventListenner(&$ppsAll, &$opt) {
stream_set_blocking($ppsAll[2], $blk); $prs = array();
$retVal = 255;
$out = "";
$err = "";
if($blk) {
if(@$opt["stdin"]) {
fwrite($ppsAll[0], $opt["stdin"]);
fclose($ppsAll[0]);
}
if(@$opt["stdOutRead"]) {
$this->d("Read loop");
while(!feof($ppsAll[1])) { while(!feof($ppsAll[1])) {
$out = stream_get_contents($ppsAll[1], 1048576); $read = [ $ppsAll[1], $ppsAll[2] ];
// $err = stream_get_contents($ppsAll[2], 1048576); $write = null;
$except = null;
($opt["stdOutRead" ]["call"]) ( // $this->d("Add stream event listenner");
$out, $cnt = stream_select(
$opt["stdOutRead"] $read,
$write,
$except,
1, // seconds
0 // microseconds
); );
}
} if(false === $cnt) {
else { $this->d("stream_select err");
$out = stream_get_contents($ppsAll[1], 1048576); return false;
$err = stream_get_contents($ppsAll[2], 1048576);
} }
fclose($ppsAll[1]); // $this->d("Streams event count : " . $sel);
fclose($ppsAll[2]);
}
else {
// $this->d("Nonblock");
if(!$cnt) {
continue;
}
$out = stream_get_contents($ppsAll[1]);
if($out) {
$prs["streamCurrent"] = "stdout";
if(!$this->cmpSysExecEventString($prs, $out, $opt))
return false;
}
$err = stream_get_contents($ppsAll[2]);
if($err) {
$prs["streamCurrent"] = "stderr";
if(!$this->cmpSysExecEventString($prs, $err, $opt))
return false;
}
// while
}
return true;
}
function cmpSysExecEventSwo(&$ppsAll, &$opt) {
do { do {
if(!@$opt["stdInEventAdd"]) if(!@$opt["stdInEventAdd"])
break; break;
@@ -429,8 +470,86 @@
($opt["eventWait"]["call"])(); ($opt["eventWait"]["call"])();
} while(0); } while(0);
return true;
} }
function cmpSysExec($cmaRaw, $opt = []) {
$cmdStr = $this->cmpShellEsc($cmaRaw);
if(!@$opt["silent"]) {
$this->d("cmpSysExec: " . $cmdStr);
}
$fdsAll = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$workDir = "/tmp";
if(@$opt["workDir"])
$workDir = $opt["workDir"];
$envAll = null;
$ppsAll = array();
$prcRes = proc_open($cmdStr, $fdsAll, $ppsAll, $workDir, $envAll);
if(!is_resource($prcRes)) {
$this->e("Can't create process");
return null;
}
$blk = 1;
if(isset($opt["blocking"])) {
$blk = $opt["blocking"];
}
$retVal = 255;
$out = "";
$err = "";
if(isset($opt["eventString"]) && $opt["eventString"]) {
stream_set_blocking($ppsAll[1], 0);
stream_set_blocking($ppsAll[2], 0);
if(@$opt["stdin"]) {
fwrite($ppsAll[0], $opt["stdin"]);
fclose($ppsAll[0]);
}
$this->cmpSysExecEventListenner($ppsAll, $opt);
}
else if($blk) {
stream_set_blocking($ppsAll[1], $blk);
stream_set_blocking($ppsAll[2], $blk);
if(@$opt["stdin"]) {
fwrite($ppsAll[0], $opt["stdin"]);
fclose($ppsAll[0]);
}
$out = stream_get_contents($ppsAll[1], 1048576);
$err = stream_get_contents($ppsAll[2], 1048576);
}
else {
stream_set_blocking($ppsAll[1], $blk);
stream_set_blocking($ppsAll[2], $blk);
// $this->d("Nonblock");
$this->cmpSysExecEventSwo($ppsAll, $opt);
}
// $this->d("Close all pipe");
fclose($ppsAll[1]);
fclose($ppsAll[2]);
$retVal = proc_close($prcRes); $retVal = proc_close($prcRes);