0
我有一個小的PHP編寫的CLI腳本,它可以作爲Linux的基於CLI的calc
的前端工作。腳本從用戶獲取數學表達式並將它們傳遞給calc
。然後,當用戶想要退出時,他只需輸入stop
。在這種情況下,腳本將exit
發送到calc
。這個腳本的問題是它只在用戶發送stop
時才顯示輸出。但我需要輸出每個用戶的數學表達式。下面的腳本是:輸出問題proc_open()
<?php
define('BUFSIZ', 1024);
define('EXIT_CMD', 'stop');
function printOutput(&$fd) {
while (!feof($fd)) {
echo fgets($fd, BUFSIZ);
}
}
function &getDescriptorSpec()
{
$spec = array(
0 => array("pty"), // stdin
1 => array("pty"), // stdout
2 => array("pty") // stderr
);
return $spec;
}
function readInputLine(&$fd)
{
echo "Enter your input\n";
$line = trim(fgets($fd));
return $line;
}
function sendCmd(&$fd, $cmd)
{
fwrite($fd, "${cmd}\n");
}
function main() {
$spec = getDescriptorSpec();
$process = proc_open("calc", $spec, $pipes);
if (is_resource($process)) {
$procstdin = &$pipes[0];
$procstdout = &$pipes[1];
$fp = fopen('php://stdin', 'r');
while (TRUE) {
$line = readInputLine($fp);
if (0 === strcmp($line, EXIT_CMD)) {
break;
}
sendCmd($procstdin, $line);
}
sendCmd($procstdin, "exit");
fclose($procstdin);
printOutput($procstdout);
fclose($procstdout);
$retval = proc_close($process);
echo "retval = $retval\n";
fclose($fp);
}
}
main();
嘗試使用'flush()'作爲輸出最有可能被緩衝。 –
是的,'flush()'+一些代碼修改做到了!你知道我在哪裏可以發佈修改後的代碼,現在可以工作嗎? –