bit more work on menu

implement helper menu
name some macros to help with declarations (sorry Felipe :))
This commit is contained in:
krakjoe 2013-11-10 10:44:42 +00:00
parent 39263cbfb1
commit 2089caed53
6 changed files with 207 additions and 42 deletions

View file

@ -9,7 +9,7 @@ if test "$PHP_PHPDBG" != "no"; then
AC_DEFINE(HAVE_PHPDBG, 1, [ ]) AC_DEFINE(HAVE_PHPDBG, 1, [ ])
PHP_PHPDBG_CFLAGS=-I$abs_srcdir/sapi/phpdbg PHP_PHPDBG_CFLAGS=-I$abs_srcdir/sapi/phpdbg
PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c" PHP_PHPDBG_FILES="phpdbg.c phpdbg_prompt.c phpdbg_help.c"
PHP_ADD_MAKEFILE_FRAGMENT([$abs_srcdir/sapi/phpdbg/Makefile.frag]) PHP_ADD_MAKEFILE_FRAGMENT([$abs_srcdir/sapi/phpdbg/Makefile.frag])
PHP_SELECT_SAPI(phpdbg, program, $PHP_PHPDBG_FILES, $PHP_PHPDBG_CFLAGS, [$(SAPI_PHPDBG_PATH)]) PHP_SELECT_SAPI(phpdbg, program, $PHP_PHPDBG_FILES, $PHP_PHPDBG_CFLAGS, [$(SAPI_PHPDBG_PATH)])

View file

@ -16,27 +16,40 @@
+----------------------------------------------------------------------+ +----------------------------------------------------------------------+
*/ */
#include "php.h" #include "phpdbg.h"
#include "php_globals.h"
#include "php_variables.h" ZEND_DECLARE_MODULE_GLOBALS(phpdbg);
#include "zend_modules.h"
#include "php.h" static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) {}
#include "zend_ini_scanner.h"
#include "zend_globals.h" static PHP_MINIT_FUNCTION(phpdbg) {
#include "zend_stream.h" ZEND_INIT_MODULE_GLOBALS(phpdbg, php_phpdbg_globals_ctor, NULL);
#include "SAPI.h"
#include <php_config.h> return SUCCESS;
#include "php_main.h" }
#include "phpdbg_prompt.h"
static inline void php_phpdbg_destroy_break(void *brake) {
}
static PHP_RINIT_FUNCTION(phpdbg) {
zend_hash_init(&PHPDBG_G(breaks), 8, NULL, php_phpdbg_destroy_break, 0);
return SUCCESS;
}
static PHP_RSHUTDOWN_FUNCTION(phpdbg) {
zend_hash_destroy(&PHPDBG_G(breaks));
}
static zend_module_entry sapi_phpdbg_module_entry = { static zend_module_entry sapi_phpdbg_module_entry = {
STANDARD_MODULE_HEADER, STANDARD_MODULE_HEADER,
"phpdbg", "phpdbg",
NULL, NULL,
PHP_MINIT(phpdbg),
NULL, NULL,
NULL, PHP_RINIT(phpdbg),
NULL, PHP_RSHUTDOWN(phpdbg),
NULL,
NULL, NULL,
"0.1", "0.1",
STANDARD_MODULE_PROPERTIES STANDARD_MODULE_PROPERTIES
@ -121,7 +134,7 @@ int main(int argc, char **argv) /* {{{ */
} zend_end_try(); } zend_end_try();
zend_try { zend_try {
phpdbg_iteractive(argc, argv); phpdbg_interactive(argc, argv TSRMLS_CC);
} zend_end_try(); } zend_end_try();
if (PG(modules_activated)) { if (PG(modules_activated)) {

38
phpdbg_help.c Normal file
View file

@ -0,0 +1,38 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <string.h>
#include "zend.h"
#include "phpdbg.h"
#include "phpdbg_help.h"
PHPDBG_HELP(print) /* {{{ */
{
printf("doing print help: %s\n", expr);
return SUCCESS;
} /* }}} */
PHPDBG_HELP(brake) /* {{{ */
{
printf("doing brake help: %s\n", expr);
return SUCCESS;
} /* }}} */

46
phpdbg_help.h Normal file
View file

@ -0,0 +1,46 @@
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#ifndef PHPDBG_HELP_H
#define PHPDBG_HELP_H
/**
* Command Declarators
*/
#define PHPDBG_HELP_D(name, tip) \
{PHPDBG_STRL(#name), tip, sizeof(tip)-1, phpdbg_do_help_##name}
#define PHPDBG_HELP(name) \
int phpdbg_do_help_##name(const char *expr, size_t expr_len TSRMLS_DC)
/**
* Helper Forward Declarations
*/
PHPDBG_HELP(print);
PHPDBG_HELP(brake);
/**
* Commands
*/
static const phpdbg_command_t phpdbg_help_commands[] = {
PHPDBG_HELP_D(print, "printing allows inspection of the execution environment"),
PHPDBG_HELP_D(brake, "brake points allow execution interruption"),
{NULL, 0, 0}
};
#endif /* PHPDBG_HELP_H */

View file

@ -13,46 +13,94 @@
| license@php.net so we can mail you a copy immediately. | | license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+ +----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> | | Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+ +----------------------------------------------------------------------+
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "zend.h" #include "zend.h"
#include "phpdbg_prompt.h" #include "phpdbg.h"
#include "phpdbg_help.h"
static void do_quit(const char *params) /* {{{ */ static const phpdbg_command_t phpdbg_prompt_commands[];
{
zend_bailout(); static PHPDBG_COMMAND(print) { /* {{{ */
printf("%s", expr);
return SUCCESS;
} /* }}} */ } /* }}} */
static const phpdbg_command prompt_commands[] = { static PHPDBG_COMMAND(brake) { /* {{{ */
{PHPDBG_STRL("quit"), do_quit}, return SUCCESS;
} /* }}} */
static PHPDBG_COMMAND(quit) /* {{{ */
{
zend_bailout();
return SUCCESS;
} /* }}} */
static PHPDBG_COMMAND(help) /* {{{ */
{
printf("Welcome to phpdbg, the interactive PHP debugger.\n");
if (!expr_len) {
printf("To get help regarding a specific command type \"help command\"\n");
printf("Commands:\n");
{
const phpdbg_command_t *command = phpdbg_prompt_commands;
while (command && command->name) {
printf(
"\t%s\t%s\n", command->name, command->tip);
command++;
}
}
printf("Helpers Loaded:\n");
{
const phpdbg_command_t *command = phpdbg_help_commands;
while (command && command->name) {
printf(
"\t%s\t%s\n", command->name, command->tip);
command++;
}
}
} else {
if (phpdbg_do_cmd(phpdbg_help_commands, expr, expr_len TSRMLS_CC) == FAILURE) {
printf("failed to find help command: %s\n", expr);
}
}
printf("Please report bugs to <http://theman.in/themoon>\n");
return SUCCESS;
} /* }}} */
static const phpdbg_command_t phpdbg_prompt_commands[] = {
PHPDBG_COMMAND_D(print, "print something"),
PHPDBG_COMMAND_D(brake, "set brake point"),
PHPDBG_COMMAND_D(help, "show help menu"),
PHPDBG_COMMAND_D(quit, "exit phpdbg"),
{NULL, 0, 0} {NULL, 0, 0}
}; };
static void do_cmd(char *cmd_line) /* {{{ */ int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC) /* {{{ */
{ {
const phpdbg_command *command = prompt_commands;
char *params = NULL; char *params = NULL;
const char *cmd = strtok_r(cmd_line, " ", &params); const char *cmd = strtok_r(cmd_line, " ", &params);
size_t cmd_len = cmd ? strlen(cmd) : 0; size_t expr_len = cmd != NULL ? strlen(cmd) : 0;
while (command && command->name) { while (command && command->name) {
if (command->name_len == cmd_len if (command->name_len == expr_len
&& memcmp(cmd, command->name, cmd_len) == 0) { && memcmp(cmd, command->name, expr_len) == 0) {
/* Command found! */ return command->handler(params, cmd_len - expr_len TSRMLS_CC);
command->handler(params);
return;
} }
++command; ++command;
} }
printf("command not found!\n"); return FAILURE;
} /* }}} */ } /* }}} */
void phpdbg_iteractive(int argc, char **argv) /* {{{ */ void phpdbg_interactive(int argc, char **argv TSRMLS_DC) /* {{{ */
{ {
char cmd[PHPDBG_MAX_CMD]; char cmd[PHPDBG_MAX_CMD];
@ -61,12 +109,16 @@ void phpdbg_iteractive(int argc, char **argv) /* {{{ */
while (fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL) { while (fgets(cmd, PHPDBG_MAX_CMD, stdin) != NULL) {
size_t cmd_len = strlen(cmd) - 1; size_t cmd_len = strlen(cmd) - 1;
if (cmd[cmd_len] == '\n') { while (cmd[cmd_len] == '\n') {
cmd[cmd_len] = 0; cmd[cmd_len] = 0;
} }
if (cmd_len) { if (cmd_len) {
do_cmd(cmd); if (phpdbg_do_cmd(phpdbg_prompt_commands, cmd, cmd_len TSRMLS_CC) == FAILURE) {
printf("error executing %s !\n", cmd);
}
} }
printf("phpdbg> "); printf("phpdbg> ");
} }
} /* }}} */ } /* }}} */

View file

@ -13,6 +13,7 @@
| license@php.net so we can mail you a copy immediately. | | license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+ +----------------------------------------------------------------------+
| Authors: Felipe Pena <felipe@php.net> | | Authors: Felipe Pena <felipe@php.net> |
| Authors: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+ +----------------------------------------------------------------------+
*/ */
@ -29,17 +30,32 @@
/** /**
* Command handler * Command handler
*/ */
typedef void (*phpdbg_command_handler)(const char*); typedef int (*phpdbg_command_handler_t)(const char* expr, size_t expr_len TSRMLS_DC);
/** /**
* Command representation * Command representation
*/ */
typedef struct _phpdbg_command { typedef struct _phpdbg_command_t {
const char *name; /* Command name */ const char *name; /* Command name */
size_t name_len; /* Command name length */ size_t name_len; /* Command name length */
phpdbg_command_handler handler; /* Command handler */ const char *tip; /* Menu tip */
} phpdbg_command; size_t tip_len; /* Menu tip length */
phpdbg_command_handler_t handler; /* Command handler */
} phpdbg_command_t;
void phpdbg_iteractive(int, char**); /**
* Command Executor
*/
int phpdbg_do_cmd(const phpdbg_command_t *command, char *cmd_line, size_t cmd_len TSRMLS_DC);
/**
* Command Declarators
*/
#define PHPDBG_COMMAND_D(name, tip) \
{PHPDBG_STRL(#name), tip, sizeof(tip)-1, phpdbg_do_##name}
#define PHPDBG_COMMAND(name) \
int phpdbg_do_##name(const char *expr, size_t expr_len TSRMLS_DC)
void phpdbg_interactive(int argc, char** argv TSRMLS_DC);
#endif /* PHPDBG_PROMPT_H */ #endif /* PHPDBG_PROMPT_H */