This commit is contained in:
krakjoe 2013-11-19 21:58:03 +00:00
commit 3709ea995c
4 changed files with 91 additions and 74 deletions

View File

@ -115,24 +115,24 @@ void phpdbg_clear_param(phpdbg_param_t *param TSRMLS_DC) /* {{{ */
break;
}
}
} /* }}} */
static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_DC) /* {{{ */
{
char *p, *s;
char *p;
char b[PHPDBG_MAX_CMD];
int l=0;
enum states {
IN_BETWEEN,
IN_WORD,
IN_STRING
IN_WORD,
IN_STRING
} state = IN_BETWEEN;
phpdbg_input_t **argv = NULL;
argv = (phpdbg_input_t**) emalloc(sizeof(phpdbg_input_t**));
(*argc) = 0;
#define RESET_STATE() do {\
phpdbg_input_t *arg = emalloc(sizeof(phpdbg_input_t));\
if (arg) {\
@ -157,11 +157,9 @@ static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_D
}
if (c == '"') {
state = IN_STRING;
s = p + 1;
continue;
}
state = IN_WORD;
s = p;
b[l++]=c;
continue;
@ -186,31 +184,31 @@ static inline phpdbg_input_t** phpdbg_read_argv(char *buffer, int *argc TSRMLS_D
continue;
}
}
switch (state) {
case IN_WORD: {
RESET_STATE();
} break;
case IN_STRING:
phpdbg_error(
"Malformed command line (unclosed quote) @ %d: %s!",
"Malformed command line (unclosed quote) @ %d: %s!",
(p - buffer)-1, &buffer[(p - buffer)-1]);
break;
}
if ((*argc) == 0) {
/* not needed */
efree(argv);
/* to be sure */
return NULL;
}
return argv;
} /* }}} */
phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
phpdbg_input_t *phpdbg_read_input(TSRMLS_D) /* {{{ */
{
if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
phpdbg_input_t *buffer = NULL;
@ -218,7 +216,7 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
#ifndef HAVE_LIBREADLINE
char *cmd = NULL;
char buf[PHPDBG_MAX_CMD];
char buf[PHPDBG_MAX_CMD];
if (!phpdbg_write(PROMPT) ||
!fgets(buf, PHPDBG_MAX_CMD, stdin)) {
/* the user has gone away */
@ -227,7 +225,7 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
zend_bailout();
return NULL;
}
cmd = buf;
#else
char *cmd = readline(PROMPT);
@ -242,46 +240,36 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
add_history(cmd);
#endif
/* strip whitespace */
while (cmd && isspace(*cmd))
cmd++;
cmd_len = strlen(cmd);
while (*cmd && isspace(cmd[cmd_len-1]))
cmd_len--;
cmd[cmd_len] = '\0';
/* allocate and sanitize buffer */
buffer = (phpdbg_input_t*) emalloc(sizeof(phpdbg_input_t));
if (buffer) {
buffer->length = strlen(cmd);
buffer->string = emalloc(buffer->length+1);
if (buffer->string) {
memcpy(
buffer->string, cmd, buffer->length);
buffer->string[buffer->length] = '\0';
/* store constant pointer to start of buffer */
buffer->start = (char* const*) buffer->string;
{
/* temporary, when we switch to argv/argc handling
will be unnecessary */
char *store = (char*) estrdup(buffer->string);
buffer->argv = phpdbg_read_argv(
store, &buffer->argc TSRMLS_CC);
if (buffer->argc) {
int arg = 0;
while (arg < buffer->argc) {
phpdbg_debug(
"argv %d=%s", arg, buffer->argv[arg]->string);
arg++;
}
}
efree(store);
if (!buffer) {
return NULL;
}
buffer->string = phpdbg_trim(cmd, strlen(cmd), &buffer->length);
if (buffer->string) {
/* temporary, when we switch to argv/argc handling
will be unnecessary */
char *store = (char*) estrdup(buffer->string);
/* store constant pointer to start of buffer */
buffer->start = (char* const*) buffer->string;
buffer->argv = phpdbg_read_argv(
store, &buffer->argc TSRMLS_CC);
if (buffer->argc) {
int arg = 0;
while (arg < buffer->argc) {
phpdbg_debug(
"argv %d=%s", arg, buffer->argv[arg]->string);
arg++;
}
}
efree(store);
}
#ifdef HAVE_LIBREADLINE
@ -292,18 +280,17 @@ phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
return buffer;
}
return NULL;
} /* }}} */
void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
{
if (*input) {
if ((*input)->string) {
efree((*input)->string);
}
if ((*input)->argc > 0) {
int arg;
for (arg=0; arg<(*input)->argc; arg++) {
@ -315,7 +302,7 @@ void phpdbg_destroy_input(phpdbg_input_t **input TSRMLS_DC) /*{{{ */
if ((*input)->argv) {
efree((*input)->argv);
}
efree(*input);
}
} /* }}} */
@ -326,10 +313,10 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
if (input->argc > 0) {
while (command && command->name && command->handler) {
if (((command->name_len == input->argv[0]->length) &&
if (((command->name_len == input->argv[0]->length) &&
(memcmp(command->name, input->argv[0]->string, command->name_len) == SUCCESS)) ||
(command->alias &&
(input->argv[0]->length == 1) &&
(input->argv[0]->length == 1) &&
(command->alias == *input->argv[0]->string))) {
phpdbg_param_t param;
@ -353,18 +340,18 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
input->argv[1]->string,
input->argv[1]->length,
&param TSRMLS_CC);
}
}
}
phpdbg_debug(
"found command \"%s\" for \"%s\" have %d arguments",
"found command %s for %s with %d arguments",
command->name, input->argv[0]->string, input->argc-1);
{
int arg;
for (arg=1; arg<input->argc; arg++) {
phpdbg_debug(
"\t#%d: [%s=%d]",
arg,
"\t#%d: [%s=%d]",
arg,
input->argv[arg]->string,
input->argv[arg]->length);
}
@ -376,7 +363,6 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
PHPDBG_G(lparam) = param;
rc = command->handler(&param TSRMLS_CC);
break;
}
command++;
@ -386,7 +372,7 @@ int phpdbg_do_cmd_ex(const phpdbg_command_t *command, phpdbg_input_t *input TSRM
phpdbg_error(
"No function executed !!");
}
return rc;
} /* }}} */
@ -409,7 +395,7 @@ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_le
expr,
(cmd_len - expr_len) ? (((cmd_len - expr_len) - sizeof(" "))+1) : 0,
&param TSRMLS_CC);
if (command->subs && param.type == STR_PARAM) {
if (phpdbg_do_cmd(command->subs, param.str, param.len TSRMLS_CC) == SUCCESS) {
rc = SUCCESS;

View File

@ -696,7 +696,7 @@ static PHPDBG_COMMAND(register) /* {{{ */
phpdbg_error(
"The requested name (%s) is already in use", lcname);
}
efree(lcname);
} break;
@ -969,7 +969,7 @@ static inline int phpdbg_call_register(phpdbg_input_t *input TSRMLS_DC) /* {{{ *
}
return SUCCESS;
}
return FAILURE;
} /* }}} */
@ -978,7 +978,7 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
int ret = SUCCESS;
phpdbg_input_t* input = phpdbg_read_input(TSRMLS_C);
if (input && input->length > 0L) {
do {
switch (ret = phpdbg_do_cmd_ex(phpdbg_prompt_commands, input TSRMLS_CC)) {
@ -1000,10 +1000,10 @@ int phpdbg_interactive(TSRMLS_D) /* {{{ */
goto out;
}
}
phpdbg_destroy_input(&input TSRMLS_CC);
} while ((input = phpdbg_read_input(TSRMLS_C)) && (input->length > 0L));
if (!input->length)
goto last;

View File

@ -50,7 +50,7 @@ int phpdbg_is_empty(const char *str) /* {{{ */
{
if (!str)
return 1;
for (; *str; str++) {
if (isspace(*str)) {
continue;
@ -71,7 +71,7 @@ int phpdbg_is_class_method(const char *str, size_t len, char **class, char **met
if (strstr(str, " ") != NULL)
return 0;
sep = strstr(str, "::");
if (!sep || sep == str || sep+2 == str+len-1) {
@ -113,6 +113,36 @@ const char *phpdbg_current_file(TSRMLS_D) /* {{{ */
return file;
} /* }}} */
char *phpdbg_trim(const char *str, size_t len, size_t *new_len) /* {{{ */
{
const char *p = str;
char *new = NULL;
while (p && isspace(*p)) {
++p;
--len;
}
while (*p && isspace(*(p + len -1))) {
--len;
}
if (len == 0) {
new = estrndup("", sizeof(""));
*new_len = 0;
} else {
new = estrndup(p, len);
*(new + len) = '\0';
if (new_len) {
*new_len = len;
}
}
return new;
} /* }}} */
int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ */
{
int rc = 0;
@ -159,7 +189,7 @@ int phpdbg_print(int type TSRMLS_DC, FILE *fp, const char *format, ...) /* {{{ *
buffer,
((PHPDBG_G(flags) & PHPDBG_IS_COLOURED) ? "\033[0m" : ""));
} break;
/* no formatting on logging output */
case P_LOG: if (buffer) {
struct timeval tp;

View File

@ -29,6 +29,7 @@ int phpdbg_is_addr(const char*);
int phpdbg_is_class_method(const char*, size_t, char**, char**);
const char *phpdbg_current_file(TSRMLS_D);
char *phpdbg_resolve_path(const char* TSRMLS_DC);
char *phpdbg_trim(const char*, size_t, size_t*);
/**
* Error/notice/formatting helper