cvs: php4 /sapi/cli php_cli.c

From: Date: Fri, 24 May 2002 01:17:37 +0000
Subject: cvs: php4 /sapi/cli php_cli.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-12066@lists.php.net to get a copy of this message
edink Thu May 23 21:17:37 2002 EDT Modified files: /php4/sapi/cli php_cli.c Log: Register STDIN, STDOUT, and STDERR "constants" in cli in cases where script itself is not being loaded from STDIN. This enables constructs like fwrite(STDERR, "Error 42"); Index: php4/sapi/cli/php_cli.c diff -u php4/sapi/cli/php_cli.c:1.25 php4/sapi/cli/php_cli.c:1.26 --- php4/sapi/cli/php_cli.c:1.25 Sat May 11 10:26:30 2002 +++ php4/sapi/cli/php_cli.c Thu May 23 21:17:37 2002 @@ -296,6 +296,51 @@ efree(*arg); } +static void cli_register_file_handles(TSRMLS_D) +{ + zval *zin, *zout, *zerr; + php_stream *s_in, *s_out, *s_err; + php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL; + zend_constant ic, oc, ec; + + MAKE_STD_ZVAL(zin); + MAKE_STD_ZVAL(zout); + MAKE_STD_ZVAL(zerr); + + s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); + s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); + s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); + + if (s_in==NULL || s_out==NULL || s_err==NULL) { + return; + } + + php_stream_to_zval(s_in, zin); + php_stream_to_zval(s_out, zout); + php_stream_to_zval(s_err, zerr); + + ic.value = *zin; + zval_copy_ctor(&ic.value); + ic.flags = CONST_CS; + ic.name = zend_strndup("STDIN", 6); + ic.name_len = 6; + zend_register_constant(&ic TSRMLS_CC); + + oc.value = *zout; + zval_copy_ctor(&oc.value); + oc.flags = CONST_CS; + oc.name = zend_strndup("STDOUT", 7); + oc.name_len = 7; + zend_register_constant(&oc TSRMLS_CC); + + ec.value = *zerr; + zval_copy_ctor(&ec.value); + ec.flags = CONST_CS; + ec.name = zend_strndup("STDERR", 7); + ec.name_len = 7; + zend_register_constant(&ec TSRMLS_CC); +} + /* {{{ main */ int main(int argc, char *argv[]) @@ -619,6 +664,9 @@ switch (behavior) { case PHP_MODE_STANDARD: + if (strcmp(file_handle.filename, "-")) { + cli_register_file_handles(TSRMLS_C); + } if (php_execute_script(&file_handle TSRMLS_CC)) { exit_status = EG(exit_status); } else { @@ -663,9 +711,11 @@ break; #endif case PHP_MODE_CLI_DIRECT: + cli_register_file_handles(TSRMLS_C); if (zend_eval_string(exec_direct, NULL, "Command line code" TSRMLS_CC) == FAILURE) { exit_status=254; } + break; } php_request_shutdown((void *) 0);

« previous php.cvs (#12066) next »