Skip to content

Commit bcf8525

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

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

phpdbg.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
# include <sys/select.h>
3737
# include <sys/time.h>
3838
# include <sys/types.h>
39+
# include <netinet/in.h>
3940
# include <unistd.h>
4041
# include <arpa/inet.h>
4142
#endif /* }}} */
@@ -532,6 +533,69 @@ static inline void php_sapi_phpdbg_flush(void *context) /* {{{ */
532533
fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
533534
} /* }}} */
534535

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+
535599
/* {{{ sapi_module_struct phpdbg_sapi_module
536600
*/
537601
static sapi_module_struct phpdbg_sapi_module = {
@@ -1261,6 +1325,9 @@ int main(int argc, char **argv) /* {{{ */
12611325
/* set default prompt */
12621326
phpdbg_set_prompt(PROMPT TSRMLS_CC);
12631327

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

0 commit comments

Comments
 (0)