Skip to content

Commit b63fd6d

Browse files
committed
embed code in .phpdbginit
1 parent 8b41387 commit b63fd6d

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

.phpdbginit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
# b m my::method
1414
# e my.php
1515
# c
16+
php:
17+
18+
end;

phpdbg_prompt.c

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,71 @@ static const phpdbg_command_t phpdbg_prompt_commands[] = {
7373

7474
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
7575

76+
int phpdbg_confirm(char *message TSRMLS_DC)
77+
{
78+
char cmd[PHPDBG_MAX_CMD];
79+
80+
zend_bool confirmed = 0,
81+
result = 0;
82+
83+
phpdbg_writeln(message);
84+
85+
do {
86+
phpdbg_error("Confirm (y or n) ?");
87+
88+
if ((fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL)) {
89+
switch (cmd[0]) {
90+
case 'y':
91+
case 'Y': {
92+
result = 1;
93+
confirmed = 1;
94+
} break;
95+
96+
case 'n':
97+
case 'N': {
98+
confirmed = 1;
99+
} break;
100+
}
101+
}
102+
} while (!confirmed);
103+
104+
return result;
105+
}
106+
107+
void phpdbg_sigint_handler(int signum)
108+
{
109+
TSRMLS_FETCH();
110+
111+
phpdbg_writeln(EMPTY);
112+
113+
signal(signum, SIG_IGN);
114+
115+
if (EG(in_execution)) {
116+
if (phpdbg_confirm(
117+
"Do you really want to quit while executing ?" TSRMLS_CC)) {
118+
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
119+
120+
zend_bailout();
121+
} else {
122+
signal(
123+
SIGINT, phpdbg_sigint_handler);
124+
phpdbg_interactive(TSRMLS_C);
125+
}
126+
} else {
127+
phpdbg_notice("Interrupted ...");
128+
129+
PHPDBG_G(flags) = PHPDBG_IS_QUITTING;
130+
131+
zend_bailout();
132+
}
133+
}
134+
76135
void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TSRMLS_DC) /* {{{ */
77136
{
78137
zend_bool init_default = 0;
79138

139+
signal(SIGINT, phpdbg_sigint_handler);
140+
80141
if (!init_file && use_default) {
81142
struct stat sb;
82143

@@ -90,9 +151,13 @@ void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TS
90151
if (init_file) {
91152
FILE *fp = fopen(init_file, "r");
92153
if (fp) {
154+
int line = 1;
155+
93156
char cmd[PHPDBG_MAX_CMD];
94157
size_t cmd_len = 0L;
95-
int line = 1;
158+
char *code = NULL;
159+
size_t code_len = 0L;
160+
zend_bool in_code = 0;
96161

97162
while (fgets(cmd, PHPDBG_MAX_CMD, fp) != NULL) {
98163
cmd_len = strlen(cmd)-1;
@@ -103,15 +168,56 @@ void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TS
103168
cmd[cmd_len] = '\0';
104169

105170
if (*cmd && cmd_len > 0L && cmd[0] != '#') {
106-
switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
107-
case FAILURE:
108-
phpdbg_error(
109-
"Unrecognized command in %s:%d: %s!", init_file, line, cmd);
110-
break;
171+
if (cmd_len == 2) {
172+
if (memcmp(cmd, "<:", sizeof("<:")-1) == SUCCESS) {
173+
in_code = 1;
174+
goto next_line;
175+
} else {
176+
if (memcmp(cmd, ":>", sizeof(":>")-1) == SUCCESS) {
177+
in_code = 0;
178+
code[code_len] = '\0';
179+
{
180+
zend_eval_stringl(
181+
code, code_len, NULL, "phpdbginit code" TSRMLS_CC);
182+
}
183+
free(code);
184+
code = NULL;
185+
goto next_line;
186+
}
187+
}
188+
}
189+
190+
if (in_code) {
191+
if (code == NULL) {
192+
code = malloc(cmd_len);
193+
} else code = realloc(code, code_len + cmd_len);
194+
195+
if (code) {
196+
memcpy(
197+
&code[code_len], cmd, cmd_len);
198+
code_len += cmd_len;
199+
}
200+
goto next_line;
201+
}
202+
203+
switch (cmd[0]) {
204+
205+
default: switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
206+
case FAILURE:
207+
phpdbg_error(
208+
"Unrecognized command in %s:%d: %s!", init_file, line, cmd);
209+
break;
210+
}
111211
}
112212
}
213+
next_line:
113214
line++;
114215
}
216+
217+
if (code) {
218+
free(code);
219+
}
220+
115221
fclose(fp);
116222
} else {
117223
phpdbg_error(

phpdbg_prompt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le
6363

6464
void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TSRMLS_DC);
6565
void phpdbg_welcome(zend_bool cleaning TSRMLS_DC);
66+
int phpdbg_confirm(char *message TSRMLS_DC);
6667
int phpdbg_interactive(TSRMLS_D);
6768
void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags TSRMLS_DC);
6869
void phpdbg_clean(zend_bool full TSRMLS_DC);

0 commit comments

Comments
 (0)