#define PHPDBG_ISSUES "http://github.com/krakjoe/phpdbg/issues"
#define PHPDBG_VERSION "0.0.2-dev" /* }}} */
+/* {{{ structs */
ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
HashTable bp[PHPDBG_BREAK_TABLES]; /* break points */
char *exec; /* file to execute */
HashTable seek; /* seek oplines */
zend_ulong flags; /* phpdbg flags */
HashTable registered; /* registered */
-ZEND_END_MODULE_GLOBALS(phpdbg)
-/* }}} */
+ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */
#endif /* PHPDBG_H */
} /* }}} */
+phpdbg_input_t* phpdbg_read_input(TSRMLS_D) /* {{{ */
+{
+ if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
+ phpdbg_input_t *buffer = NULL;
+ size_t cmd_len = 0L;
+
+#ifndef HAVE_LIBREADLINE
+ char *cmd = NULL;
+ char buf[PHPDBG_MAX_CMD];
+ if (!phpdbg_write(PROMPT) ||
+ !fgets(buf, PHPDBG_MAX_CMD, stdin)) {
+ return NULL;
+ }
+
+ cmd = buf;
+#else
+ char *cmd = readline(PROMPT);
+ if (!cmd) {
+ return NULL;
+ }
+
+ 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';
+ }
+ }
+
+#ifdef HAVE_LIBREADLINE
+ if (cmd) {
+ free(cmd);
+ }
+#endif
+
+ return buffer;
+ }
+
+ return NULL;
+} /* }}} */
+
int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
{
int rc = FAILURE;
NUMERIC_PARAM
} phpdbg_param_type;
+typedef struct {
+ char *string;
+ size_t length;
+} phpdbg_input_t;
+
typedef struct _phpdbg_param {
phpdbg_param_type type;
long num;
/* }}} */
#define PHPDBG_STRL(s) s, sizeof(s)-1
+#define PHPDBG_MAX_CMD 500
/**
* Command Executor
*/
+phpdbg_input_t* phpdbg_read_input(TSRMLS_D);
int phpdbg_do_cmd(const phpdbg_command_t*, char*, size_t TSRMLS_DC);
phpdbg_param_type phpdbg_parse_param(const char*, size_t, phpdbg_param_t* TSRMLS_DC);
void phpdbg_clear_param(phpdbg_param_t* TSRMLS_DC);
#define PHPDBG_END_COMMAND {NULL, 0, NULL, 0, '\0', NULL, NULL, '\0'}
+/*
+* Default Switch Case
+*/
#define phpdbg_default_switch_case() \
default:\
phpdbg_error(\
return SUCCESS;
} /* }}} */
-static inline int phpdbg_call_register(const char *cmd, size_t cmd_len, const char * start TSRMLS_DC)
+static inline int phpdbg_call_register(const char *cmd, size_t cmd_len, const char * start TSRMLS_DC) /* {{{ */
{
size_t offset = strlen(cmd)+(sizeof(" ")-1);
}
return FAILURE;
-}
+} /* }}} */
int phpdbg_interactive(TSRMLS_D) /* {{{ */
{
- size_t cmd_len;
int ret = SUCCESS;
- char* const* start;
-
-#ifndef HAVE_LIBREADLINE
- char cmd[PHPDBG_MAX_CMD];
-
- while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING) &&
- phpdbg_write(PROMPT) &&
- (fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL)) {
- cmd_len = strlen(cmd) - 1;
-#else
- char *cmd = NULL;
-
- while (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
- cmd = readline(PROMPT);
- cmd_len = cmd ? strlen(cmd) : 0;
-#endif
-
- start = (char* const*) cmd;
-
- /* trim space from end of input */
- while (*cmd && isspace(cmd[cmd_len-1]))
- cmd_len--;
-
- /* ensure string is null terminated */
- cmd[cmd_len] = '\0';
-
- if (*cmd && cmd_len > 0L) {
-#ifdef HAVE_LIBREADLINE
- add_history(cmd);
-#endif
-
- switch (ret = phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC)) {
- case FAILURE:
- if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
- if (phpdbg_call_register(cmd, cmd_len, (const char*) start TSRMLS_CC) == FAILURE) {
- phpdbg_error("Failed to execute %s!", cmd);
- }
- }
- break;
- case PHPDBG_LEAVE:
- case PHPDBG_FINISH:
- case PHPDBG_UNTIL:
- case PHPDBG_NEXT: {
- if (!EG(in_execution)) {
- phpdbg_error("Not running");
+ phpdbg_input_t* input = phpdbg_read_input(TSRMLS_C);
+
+ if (input) {
+ char* const* saved = (char* const*) input->string;
+
+ switch (ret = phpdbg_do_cmd(phpdbg_prompt_commands, input->string, input->length TSRMLS_CC)) {
+ case FAILURE:
+ if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
+ if (phpdbg_call_register(input->string, input->length, (const char*) saved TSRMLS_CC) == FAILURE) {
+ phpdbg_error("Failed to execute %s!", input->string);
}
- goto out;
}
- }
+ break;
-#ifdef HAVE_LIBREADLINE
- if (cmd) {
- free(cmd);
- cmd = NULL;
- }
-#endif
- } else {
- if (PHPDBG_G(lcmd)) {
- ret = PHPDBG_G(lcmd)->handler(
- &PHPDBG_G(lparam) TSRMLS_CC);
+ case PHPDBG_LEAVE:
+ case PHPDBG_FINISH:
+ case PHPDBG_UNTIL:
+ case PHPDBG_NEXT: {
+ if (!EG(in_execution)) {
+ phpdbg_error("Not running");
+ }
goto out;
}
}
+ } else {
+ if (PHPDBG_G(lcmd)) {
+ ret = PHPDBG_G(lcmd)->handler(
+ &PHPDBG_G(lparam) TSRMLS_CC);
+ goto out;
+ }
}
out:
-#ifdef HAVE_LIBREADLINE
- if (cmd) {
- free(cmd);
+ if (input) {
+ if (input->string) {
+ efree(input->string);
+ }
+ efree(input);
}
-#endif
return ret;
} /* }}} */
/**
* Maximum command length
*/
-#define PHPDBG_MAX_CMD 500
-
void phpdbg_init(char *init_file, size_t init_file_len, zend_bool use_default TSRMLS_DC);
int phpdbg_interactive(TSRMLS_D);
void phpdbg_print_opline(zend_execute_data *execute_data, zend_bool ignore_flags TSRMLS_DC);