Skip to content

Commit 6417e47

Browse files
committed
use new input handling everywhere
1 parent 3709ea9 commit 6417e47

File tree

3 files changed

+48
-89
lines changed

3 files changed

+48
-89
lines changed

phpdbg_cmd.c

Lines changed: 31 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */
118118

119119
} /* }}} */
120120

121-
static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC) /* {{{ */
121+
phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC) /* {{{ */
122122
{
123123
char *p;
124124
char b[PHPDBG_MAX_CMD];
@@ -208,37 +208,39 @@ static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_D
208208
return argv;
209209
} /* }}} */
210210

211-
phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */
211+
phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
212212
{
213-
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
214-
phpdbg_input_t *buffer = NULL;
215-
size_t cmd_len = 0L;
213+
phpdbg_input_t *buffer = NULL;
214+
size_t cmd_len = 0L;
215+
char *cmd = NULL;
216216

217+
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
218+
if (buffered == NULL) {
217219
#ifndef HAVE_LIBREADLINE
218-
char *cmd = NULL;
219-
char buf[PHPDBG_MAX_CMD];
220-
if (!phpdbg_write(PROMPT) ||
221-
!fgets(buf, PHPDBG_MAX_CMD, stdin)) {
222-
/* the user has gone away */
223-
phpdbg_error("Failed to read console !");
224-
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
225-
zend_bailout();
226-
return NULL;
227-
}
220+
char buf[PHPDBG_MAX_CMD];
221+
if (!phpdbg_write(PROMPT) ||
222+
!fgets(buf, PHPDBG_MAX_CMD, stdin)) {
223+
/* the user has gone away */
224+
phpdbg_error("Failed to read console !");
225+
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
226+
zend_bailout();
227+
return NULL;
228+
}
228229

229-
cmd = buf;
230+
cmd = buf;
230231
#else
231-
char *cmd = readline(PROMPT);
232-
if (!cmd) {
233-
/* the user has gone away */
234-
phpdbg_error("Failed to read console !");
235-
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
236-
zend_bailout();
237-
return NULL;
238-
}
232+
cmd = readline(PROMPT);
233+
if (!cmd) {
234+
/* the user has gone away */
235+
phpdbg_error("Failed to read console !");
236+
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
237+
zend_bailout();
238+
return NULL;
239+
}
239240

240-
add_history(cmd);
241+
add_history(cmd);
241242
#endif
243+
} else cmd = buffered;
242244

243245
/* allocate and sanitize buffer */
244246
buffer = (phpdbg_input_t*) emalloc(sizeof(phpdbg_input_t));
@@ -273,7 +275,7 @@ phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */
273275
}
274276

275277
#ifdef HAVE_LIBREADLINE
276-
if (cmd) {
278+
if (!buffered && cmd) {
277279
free(cmd);
278280
}
279281
#endif
@@ -307,7 +309,7 @@ void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
307309
}
308310
} /* }}} */
309311

310-
int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRMLS_DC) /* {{{ */
312+
int phpdbg_do_cmd(const phpdbg_command_t *command, phpdbg_input_t *input TSRMLS_DC) /* {{{ */
311313
{
312314
int rc = FAILURE;
313315

@@ -334,13 +336,13 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
334336
"trying sub commands in \"%s\" for \"%s\" with %d arguments",
335337
command->name, sub.argv[0]->string, sub.argc-1);
336338

337-
return phpdbg_do_cmd_ex(command->subs, &sub TSRMLS_CC);
339+
return phpdbg_do_cmd(command->subs, &sub TSRMLS_CC);
338340
} else {
339341
phpdbg_parse_param(
340342
input->argv[1]->string,
341343
input->argv[1]->length,
342344
&param TSRMLS_CC);
343-
}
345+
}
344346
}
345347

346348
phpdbg_debug(
@@ -376,51 +378,3 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
376378
return rc;
377379
} /* }}} */
378380

379-
int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
380-
{
381-
int rc = FAILURE;
382-
char *expr = NULL;
383-
#ifndef _WIN32
384-
const char *cmd = strtok_r(cmd_line, " ", &expr);
385-
#else
386-
const char *cmd = strtok_s(cmd_line, " ", &expr);
387-
#endif
388-
size_t expr_len = (cmd != NULL) ? strlen(cmd) : 0;
389-
390-
while (command && command->name && command->handler) {
391-
if ((command->name_len == expr_len && memcmp(cmd, command->name, expr_len) == 0)
392-
|| (expr_len == 1 && command->alias && command->alias == cmd_line[0])) {
393-
phpdbg_param_t param;
394-
phpdbg_parse_param(
395-
expr,
396-
(cmd_len - expr_len) ? (((cmd_len - expr_len) - sizeof(" "))+1) : 0,
397-
&param TSRMLS_CC);
398-
399-
if (command->subs && param.type == STR_PARAM) {
400-
if (phpdbg_do_cmd(command->subs, param.str, param.len TSRMLS_CC) == SUCCESS) {
401-
rc = SUCCESS;
402-
goto done;
403-
}
404-
}
405-
406-
if (command->arg_type == REQUIRED_ARG && param.type == EMPTY_PARAM) {
407-
phpdbg_error("This command requires argument!");
408-
rc = FAILURE;
409-
} else if (command->arg_type == NO_ARG && param.type != EMPTY_PARAM) {
410-
phpdbg_error("This command does not expect argument!");
411-
rc = FAILURE;
412-
} else {
413-
PHPDBG_G(lcmd) = (phpdbg_command_t*) command;
414-
phpdbg_clear_param(
415-
&PHPDBG_G(lparam) TSRMLS_CC);
416-
PHPDBG_G(lparam) = param;
417-
rc = command->handler(&param TSRMLS_CC);
418-
}
419-
break;
420-
}
421-
++command;
422-
}
423-
424-
done:
425-
return rc;
426-
} /* }}} */

phpdbg_cmd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ struct _phpdbg_command_t {
8787
/**
8888
* Command Executor
8989
*/
90-
phpdbg_input_t* phpdbg_read_input(TSRMLS_D);
91-
int phpdbg_do_cmd_ex(const phpdbg_command_t*, phpdbg_input_t *input TSRMLS_DC);
92-
int phpdbg_do_cmd(const phpdbg_command_t*, char*, size_t TSRMLS_DC);
90+
phpdbg_input_t* phpdbg_read_input(char *buffered TSRMLS_DC);
91+
phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC);
92+
int phpdbg_do_cmd(const phpdbg_command_t*, phpdbg_input_t *input TSRMLS_DC);
9393
phpdbg_param_type phpdbg_parse_param(const char*, size_t, phpdbg_param_t* TSRMLS_DC);
9494
void phpdbg_clear_param(phpdbg_param_t* TSRMLS_DC);
9595
const char* phpdbg_get_param_type(const phpdbg_param_t* TSRMLS_DC);

phpdbg_prompt.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,17 @@ void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TS
154154
goto next_line;
155155
}
156156

157-
switch (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
158-
case FAILURE:
159-
phpdbg_error(
160-
"Unrecognized command in %s:%d: %s!", init_file, line, cmd);
161-
break;
157+
{
158+
phpdbg_input_t *input = phpdbg_read_input(cmd TSRMLS_CC);
159+
switch (phpdbg_do_cmd(phpdbg_prompt_commands, input TSRMLS_CC)) {
160+
case FAILURE:
161+
phpdbg_error(
162+
"Unrecognized command in %s:%d: %s!", init_file, line, cmd);
163+
break;
164+
}
165+
phpdbg_destroy_input(&input TSRMLS_CC);
162166
}
167+
163168
}
164169
next_line:
165170
line++;
@@ -977,11 +982,11 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
977982
{
978983
int ret = SUCCESS;
979984

980-
phpdbg_input_t* input = phpdbg_read_input(TSRMLS_C);
985+
phpdbg_input_t* input = phpdbg_read_input(NULL TSRMLS_CC);
981986

982987
if (input && input->length > 0L) {
983988
do {
984-
switch (ret = phpdbg_do_cmd_ex(phpdbg_prompt_commands, input TSRMLS_CC)) {
989+
switch (ret = phpdbg_do_cmd(phpdbg_prompt_commands, input TSRMLS_CC)) {
985990
case FAILURE:
986991
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
987992
if (phpdbg_call_register(input TSRMLS_CC) == FAILURE) {
@@ -1002,9 +1007,9 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
10021007
}
10031008

10041009
phpdbg_destroy_input(&input TSRMLS_CC);
1005-
} while ((input = phpdbg_read_input(TSRMLS_C)) && (input->length > 0L));
1010+
} while ((input = phpdbg_read_input(NULL TSRMLS_CC)) && (input->length > 0L));
10061011

1007-
if (!input->length)
1012+
if (input && !input->length)
10081013
goto last;
10091014

10101015
} else {

0 commit comments

Comments
 (0)