Skip to content

Commit cc70ec1

Browse files
committed
Add question to reset execution in run/exec/clean
1 parent 25e3560 commit cc70ec1

File tree

7 files changed

+170
-138
lines changed

7 files changed

+170
-138
lines changed

sapi/phpdbg/phpdbg.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#endif /* }}} */
4646

4747
ZEND_DECLARE_MODULE_GLOBALS(phpdbg);
48+
int phpdbg_startup_run = 0;
49+
char *phpdbg_exec = NULL;
4850

4951
static PHP_INI_MH(OnUpdateEol)
5052
{
@@ -487,7 +489,7 @@ static void php_sapi_phpdbg_log_message(char *message TSRMLS_DC) /* {{{ */
487489
case PHPDBG_NEXT:
488490
return;
489491
}
490-
} while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING));
492+
} while (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING));
491493

492494
}
493495
} else fprintf(stdout, "%s\n", message);
@@ -752,7 +754,7 @@ static void phpdbg_welcome(zend_bool cleaning TSRMLS_DC) /* {{{ */
752754
phpdbg_writeln("intro", "help=\"help\"", "To get help using phpdbg type \"help\" and press enter");
753755
phpdbg_notice("intro", "report=\"%s\"", "Please report bugs to <%s>", PHPDBG_ISSUES);
754756
phpdbg_xml("</intros>");
755-
} else {
757+
} else if (phpdbg_startup_run == 0) {
756758
if (!(PHPDBG_G(flags) & PHPDBG_WRITE_XML)) {
757759
phpdbg_notice(NULL, NULL, "Clean Execution Environment");
758760
}
@@ -776,7 +778,7 @@ static inline void phpdbg_sigint_handler(int signo) /* {{{ */
776778
if (PHPDBG_G(flags) & PHPDBG_IS_INTERACTIVE) {
777779
/* we quit remote consoles on recv SIGINT */
778780
if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE) {
779-
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
781+
PHPDBG_G(flags) |= PHPDBG_IS_STOPPING;
780782
zend_bailout();
781783
}
782784
} else {
@@ -961,8 +963,6 @@ int main(int argc, char **argv) /* {{{ */
961963
zend_ulong zend_extensions_len = 0L;
962964
zend_bool ini_ignore;
963965
char *ini_override;
964-
char *exec;
965-
size_t exec_len;
966966
char *init_file;
967967
size_t init_file_len;
968968
zend_bool init_file_default;
@@ -973,7 +973,6 @@ int main(int argc, char **argv) /* {{{ */
973973
int php_optind, opt, show_banner = 1;
974974
long cleaning = 0;
975975
zend_bool remote = 0;
976-
int run = 0;
977976
int step = 0;
978977

979978
#ifdef _WIN32
@@ -1046,8 +1045,6 @@ int main(int argc, char **argv) /* {{{ */
10461045
ini_override = NULL;
10471046
zend_extensions = NULL;
10481047
zend_extensions_len = 0L;
1049-
exec = NULL;
1050-
exec_len = 0;
10511048
init_file = NULL;
10521049
init_file_len = 0;
10531050
init_file_default = 1;
@@ -1057,14 +1054,13 @@ int main(int argc, char **argv) /* {{{ */
10571054
php_optarg = NULL;
10581055
php_optind = 1;
10591056
opt = 0;
1060-
run = 0;
10611057
step = 0;
10621058
sapi_name = NULL;
10631059

10641060
while ((opt = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
10651061
switch (opt) {
10661062
case 'r':
1067-
run++;
1063+
phpdbg_startup_run++;
10681064
break;
10691065
case 'n':
10701066
ini_ignore = 1;
@@ -1201,14 +1197,12 @@ int main(int argc, char **argv) /* {{{ */
12011197
}
12021198

12031199
/* set exec if present on command line */
1204-
if ((argc > php_optind) && (strcmp(argv[php_optind-1],"--") != SUCCESS))
1205-
{
1206-
exec_len = strlen(argv[php_optind]);
1207-
if (exec_len) {
1208-
if (exec) {
1209-
free(exec);
1200+
if (!phpdbg_exec && (argc > php_optind) && (strcmp(argv[php_optind-1],"--") != SUCCESS)) {
1201+
if (strlen(argv[php_optind])) {
1202+
if (phpdbg_exec) {
1203+
free(phpdbg_exec);
12101204
}
1211-
exec = strdup(argv[php_optind]);
1205+
phpdbg_exec = strdup(argv[php_optind]);
12121206
}
12131207
php_optind++;
12141208
}
@@ -1331,7 +1325,7 @@ int main(int argc, char **argv) /* {{{ */
13311325
for (i = SG(request_info).argc; --i;) {
13321326
SG(request_info).argv[i] = estrdup(argv[php_optind - 1 + i]);
13331327
}
1334-
SG(request_info).argv[i] = exec ? estrndup(exec, exec_len) : estrdup("");
1328+
SG(request_info).argv[i] = phpdbg_exec ? estrdup(phpdbg_exec) : estrdup("");
13351329

13361330
php_hash_environment(TSRMLS_C);
13371331
}
@@ -1394,11 +1388,12 @@ int main(int argc, char **argv) /* {{{ */
13941388
php_stream_stdio_ops.write = phpdbg_stdiop_write;
13951389
#endif
13961390

1397-
if (exec) { /* set execution context */
1398-
PHPDBG_G(exec) = phpdbg_resolve_path(exec TSRMLS_CC);
1391+
if (phpdbg_exec) { /* set execution context */
1392+
PHPDBG_G(exec) = phpdbg_resolve_path(phpdbg_exec TSRMLS_CC);
13991393
PHPDBG_G(exec_len) = strlen(PHPDBG_G(exec));
14001394

1401-
free(exec);
1395+
free(phpdbg_exec);
1396+
phpdbg_exec = NULL;
14021397
}
14031398

14041399
if (oplog_file) { /* open oplog */
@@ -1448,13 +1443,14 @@ int main(int argc, char **argv) /* {{{ */
14481443
PHPDBG_G(flags) |= PHPDBG_IS_STEPPING;
14491444
}
14501445

1451-
if (run) {
1446+
if (phpdbg_startup_run) {
14521447
/* no need to try{}, run does it ... */
14531448
PHPDBG_COMMAND_HANDLER(run)(NULL TSRMLS_CC);
1454-
if (run > 1) {
1449+
if (phpdbg_startup_run > 1) {
14551450
/* if -r is on the command line more than once just quit */
14561451
goto phpdbg_out;
14571452
}
1453+
phpdbg_startup_run = 0;
14581454
}
14591455

14601456
phpdbg_interact:
@@ -1494,7 +1490,7 @@ int main(int argc, char **argv) /* {{{ */
14941490
}
14951491
}
14961492
} zend_end_try();
1497-
} while(!cleaning && !(PHPDBG_G(flags) & PHPDBG_IS_QUITTING));
1493+
} while (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING));
14981494

14991495
/* this must be forced */
15001496
CG(unclean_shutdown) = 0;

sapi/phpdbg/phpdbg.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ int phpdbg_do_parse(phpdbg_param_t *stack, char *input TSRMLS_DC);
187187

188188
#define PHPDBG_IN_SIGNAL_HANDLER (1<<30)
189189

190-
#define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL|PHPDBG_IN_FINISH|PHPDBG_IN_LEAVE)
191-
#define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP|PHPDBG_HAS_METHOD_OPLINE_BP|PHPDBG_HAS_FILE_OPLINE_BP)
192-
#define PHPDBG_BP_MASK (PHPDBG_HAS_FILE_BP|PHPDBG_HAS_SYM_BP|PHPDBG_HAS_METHOD_BP|PHPDBG_HAS_OPLINE_BP|PHPDBG_HAS_COND_BP|PHPDBG_HAS_OPCODE_BP|PHPDBG_HAS_FUNCTION_OPLINE_BP|PHPDBG_HAS_METHOD_OPLINE_BP|PHPDBG_HAS_FILE_OPLINE_BP)
190+
#define PHPDBG_SEEK_MASK (PHPDBG_IN_UNTIL | PHPDBG_IN_FINISH | PHPDBG_IN_LEAVE)
191+
#define PHPDBG_BP_RESOLVE_MASK (PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
192+
#define PHPDBG_BP_MASK (PHPDBG_HAS_FILE_BP | PHPDBG_HAS_SYM_BP | PHPDBG_HAS_METHOD_BP | PHPDBG_HAS_OPLINE_BP | PHPDBG_HAS_COND_BP | PHPDBG_HAS_OPCODE_BP | PHPDBG_HAS_FUNCTION_OPLINE_BP | PHPDBG_HAS_METHOD_OPLINE_BP | PHPDBG_HAS_FILE_OPLINE_BP)
193+
#define PHPDBG_IS_STOPPING (PHPDBG_IS_QUITTING | PHPDBG_IS_CLEANING)
193194

194195
#ifndef _WIN32
195196
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET|PHPDBG_IS_COLOURED|PHPDBG_IS_BP_ENABLED)

sapi/phpdbg/phpdbg_cmd.c

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -796,80 +796,30 @@ PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, zend_bool allow_async
796796

797797
PHPDBG_API char *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
798798
{
799+
char buf[PHPDBG_MAX_CMD];
799800
char *cmd = NULL;
800801
char *buffer = NULL;
801802

802-
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
803+
if (!(PHPDBG_G(flags) & PHPDBG_IS_STOPPING)) {
803804
if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) && (buffered == NULL) && !phpdbg_active_sigsafe_mem(TSRMLS_C)) {
804805
fflush(PHPDBG_G(io)[PHPDBG_STDOUT].ptr);
805806
}
806807

807808
if (buffered == NULL) {
808-
if (0) {
809-
disconnect:
810-
PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
811-
zend_bailout();
812-
return NULL;
813-
}
814-
815809
#define USE_LIB_STAR (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT))
816-
817810
/* note: EOF makes readline write prompt again in local console mode - and ignored if compiled without readline */
818-
/* strongly assuming to be in blocking mode... */
819811
#if USE_LIB_STAR
820812
readline:
821813
if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE)
822814
#endif
823815
{
824-
char buf[PHPDBG_MAX_CMD];
825-
int bytes = PHPDBG_G(input_buflen), len = 0;
826-
if (PHPDBG_G(input_buflen)) {
827-
memcpy(buf, PHPDBG_G(input_buffer), bytes);
828-
}
829-
830816
phpdbg_write("prompt", "", "%s", phpdbg_get_prompt(TSRMLS_C));
831-
PHPDBG_G(last_was_newline) = 1;
832-
833-
do {
834-
int i;
835-
if (bytes <= 0) {
836-
continue;
837-
}
838-
839-
for (i = len; i < len + bytes; i++) {
840-
if (buf[i] == '\x03') {
841-
if (i != len + bytes - 1) {
842-
memmove(buf + i, buf + i + 1, len + bytes - i - 1);
843-
}
844-
len--;
845-
i--;
846-
continue;
847-
}
848-
if (buf[i] == '\n') {
849-
PHPDBG_G(input_buflen) = len + bytes - 1 - i;
850-
if (PHPDBG_G(input_buflen)) {
851-
memcpy(PHPDBG_G(input_buffer), buf + i + 1, PHPDBG_G(input_buflen));
852-
}
853-
if (i != PHPDBG_MAX_CMD - 1) {
854-
buf[i + 1] = 0;
855-
}
856-
cmd = buf;
857-
goto end;
858-
}
859-
}
860-
len += bytes;
861-
/* XXX export the timeout through INI??*/
862-
} while ((bytes = phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, buf + len, PHPDBG_MAX_CMD - len, -1 TSRMLS_CC)) > 0);
863-
864-
if (bytes <= 0) {
865-
goto disconnect;
866-
}
867-
868-
cmd = buf;
817+
phpdbg_consume_stdin_line(cmd = buf TSRMLS_CC);
869818
}
870819
#if USE_LIB_STAR
871820
else {
872821
cmd = readline(phpdbg_get_prompt(TSRMLS_C));
822+
PHPDBG_G(last_was_newline) = 1;
873823
}
874824

875825
if (!cmd) {
@@ -883,8 +833,7 @@ PHPDBG_API char *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
883833
} else {
884834
cmd = buffered;
885835
}
886-
end:
887-
PHPDBG_G(last_was_newline) = 1;
836+
888837
buffer = estrdup(cmd);
889838

890839
#if USE_LIB_STAR
@@ -922,3 +871,24 @@ PHPDBG_API void phpdbg_destroy_input(char **input TSRMLS_DC) /*{{{ */
922871
{
923872
efree(*input);
924873
} /* }}} */
874+
875+
PHPDBG_API int phpdbg_ask_user_permission(const char *question TSRMLS_DC) {
876+
if (!(PHPDBG_G(flags) & PHPDBG_WRITE_XML)) {
877+
char buf[PHPDBG_MAX_CMD];
878+
phpdbg_out("%s", question);
879+
phpdbg_out(" (type y or n): ");
880+
881+
while (1) {
882+
phpdbg_consume_stdin_line(buf TSRMLS_CC);
883+
if (buf[1] == '\n' && (buf[0] == 'y' || buf[0] == 'n')) {
884+
if (buf[0] == 'y') {
885+
return SUCCESS;
886+
}
887+
return FAILURE;
888+
}
889+
phpdbg_out("Please enter either y (yes) or n (no): ");
890+
}
891+
}
892+
893+
return SUCCESS;
894+
}

sapi/phpdbg/phpdbg_cmd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ typedef struct {
131131
*/
132132
PHPDBG_API char* phpdbg_read_input(char *buffered TSRMLS_DC);
133133
PHPDBG_API void phpdbg_destroy_input(char** TSRMLS_DC);
134+
PHPDBG_API int phpdbg_ask_user_permission(const char *question TSRMLS_DC);
134135

135136
/**
136137
* Stack Management

sapi/phpdbg/phpdbg_io.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,55 @@
4747

4848
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
4949

50+
/* is easy to generalize ... but not needed for now */
51+
PHPDBG_API int phpdbg_consume_stdin_line(char *buf TSRMLS_DC) {
52+
int bytes = PHPDBG_G(input_buflen), len = 0;
53+
54+
if (PHPDBG_G(input_buflen)) {
55+
memcpy(buf, PHPDBG_G(input_buffer), bytes);
56+
}
57+
58+
PHPDBG_G(last_was_newline) = 1;
59+
60+
do {
61+
int i;
62+
if (bytes <= 0) {
63+
continue;
64+
}
65+
66+
for (i = len; i < len + bytes; i++) {
67+
if (buf[i] == '\x03') {
68+
if (i != len + bytes - 1) {
69+
memmove(buf + i, buf + i + 1, len + bytes - i - 1);
70+
}
71+
len--;
72+
i--;
73+
continue;
74+
}
75+
if (buf[i] == '\n') {
76+
PHPDBG_G(input_buflen) = len + bytes - 1 - i;
77+
if (PHPDBG_G(input_buflen)) {
78+
memcpy(PHPDBG_G(input_buffer), buf + i + 1, PHPDBG_G(input_buflen));
79+
}
80+
if (i != PHPDBG_MAX_CMD - 1) {
81+
buf[i + 1] = 0;
82+
}
83+
return i;
84+
}
85+
}
86+
87+
len += bytes;
88+
} while ((bytes = phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, buf + len, PHPDBG_MAX_CMD - len, -1 TSRMLS_CC)) > 0);
89+
90+
if (bytes <= 0) {
91+
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING | PHPDBG_IS_DISCONNECTED;
92+
zend_bailout();
93+
return 0;
94+
}
95+
96+
return bytes;
97+
}
98+
5099
PHPDBG_API int phpdbg_consume_bytes(int sock, char *ptr, int len, int tmo TSRMLS_DC) {
51100
int got_now, i = len, j;
52101
char *p = ptr;

sapi/phpdbg/phpdbg_io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "phpdbg.h"
2323

24+
PHPDBG_API int phpdbg_consume_stdin_line(char *buf TSRMLS_DC);
25+
2426
PHPDBG_API int phpdbg_consume_bytes(int sock, char *ptr, int len, int tmo TSRMLS_DC);
2527
PHPDBG_API int phpdbg_send_bytes(int sock, const char *ptr, int len);
2628
PHPDBG_API int phpdbg_mixed_read(int sock, char *ptr, int len, int tmo TSRMLS_DC);

0 commit comments

Comments
 (0)