Skip to content

Commit eae7a94

Browse files
committed
Added stdion/stdout/stderr constsnts and their php:// wrappers
Fixes issue #85
1 parent a1edc8e commit eae7a94

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

phpdbg.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,69 @@ static inline void php_sapi_phpdbg_flush(void *context) /* {{{ */
533533
fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
534534
} /* }}} */
535535

536+
/* copied from sapi/cli/php_cli.c cli_register_file_handles */
537+
static void phpdbg_register_file_handles(TSRMLS_D) /* {{{ */
538+
{
539+
zval *zin, *zout, *zerr;
540+
php_stream *s_in, *s_out, *s_err;
541+
php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
542+
zend_constant ic, oc, ec;
543+
544+
MAKE_STD_ZVAL(zin);
545+
MAKE_STD_ZVAL(zout);
546+
MAKE_STD_ZVAL(zerr);
547+
548+
s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in);
549+
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
550+
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
551+
552+
if (s_in==NULL || s_out==NULL || s_err==NULL) {
553+
FREE_ZVAL(zin);
554+
FREE_ZVAL(zout);
555+
FREE_ZVAL(zerr);
556+
if (s_in) php_stream_close(s_in);
557+
if (s_out) php_stream_close(s_out);
558+
if (s_err) php_stream_close(s_err);
559+
return;
560+
}
561+
562+
#if PHP_DEBUG
563+
/* do not close stdout and stderr */
564+
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
565+
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
566+
#endif
567+
568+
php_stream_to_zval(s_in, zin);
569+
php_stream_to_zval(s_out, zout);
570+
php_stream_to_zval(s_err, zerr);
571+
572+
ic.value = *zin;
573+
ic.flags = CONST_CS;
574+
ic.name = zend_strndup(ZEND_STRL("STDIN"));
575+
ic.name_len = sizeof("STDIN");
576+
ic.module_number = 0;
577+
zend_register_constant(&ic TSRMLS_CC);
578+
579+
oc.value = *zout;
580+
oc.flags = CONST_CS;
581+
oc.name = zend_strndup(ZEND_STRL("STDOUT"));
582+
oc.name_len = sizeof("STDOUT");
583+
oc.module_number = 0;
584+
zend_register_constant(&oc TSRMLS_CC);
585+
586+
ec.value = *zerr;
587+
ec.flags = CONST_CS;
588+
ec.name = zend_strndup(ZEND_STRL("STDERR"));
589+
ec.name_len = sizeof("STDERR");
590+
ec.module_number = 0;
591+
zend_register_constant(&ec TSRMLS_CC);
592+
593+
FREE_ZVAL(zin);
594+
FREE_ZVAL(zout);
595+
FREE_ZVAL(zerr);
596+
}
597+
/* }}} */
598+
536599
/* {{{ sapi_module_struct phpdbg_sapi_module
537600
*/
538601
static sapi_module_struct phpdbg_sapi_module = {
@@ -1262,6 +1325,9 @@ int main(int argc, char **argv) /* {{{ */
12621325
/* set default prompt */
12631326
phpdbg_set_prompt(PROMPT TSRMLS_CC);
12641327

1328+
/* Make stdin, stdout and stderr accessible from PHP scripts */
1329+
phpdbg_register_file_handles(TSRMLS_C);
1330+
12651331
if (show_banner) {
12661332
/* print blurb */
12671333
phpdbg_welcome((cleaning > 0) TSRMLS_CC);

0 commit comments

Comments
 (0)